1

I am stuck with this problem.

I already got the currentUTCtime in seconds from the QDateTime. Problem is, I can't find a possible way to convert this into the local time in seconds. There are some QDate functions like toLocalTime() which just don't seem to work. I hope somebody here can help me.

QDateTime::currentMSecsSinceEpoch();
QDateTime currentateTime = QDateTime::currentDateTime();
QDateTime UTC(QDateTime::currentDateTimeUtc());
currentDateTime.toString().toStdString();
TimeNow = currentDateTime.toMSecsSinceEpoch()/1000;

Above is my code for the currentUTC Time in seconds.

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
MuchWow
  • 35
  • 2
  • 7

2 Answers2

1

If you just need the time in seconds since the epoch you can use QDateTime::toTime_t(); this method exists in Qt 4.7 and seems to be a part of Qt 5 from the start, too.

QDateTime::currentDateTime().toTime_t()

for local time, or for UTC

QDateTime::currentDateTimeUtc().toTime_t()
Murphy
  • 3,827
  • 4
  • 21
  • 35
0

Use QDateTime::fromTime_t, to which the documentation states:

Returns a datetime whose date and time are the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC) and converted to the given spec.

qint64 utcTime = QDateTime::currentMSecsSinceEpoch();
QDateTime localTime = QDateTime::fromTime_t(utcTime, Qt::LocalTime);
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • ah thats a nice one. Thank you! the Problem now is that there is an error saying : no matching function for call to 'QDateTime::fromTime_t(qint64&, Qt::TimeSpec)' am i missing something? – MuchWow Apr 12 '16 at 13:21
  • You must be using at least Qt 5.2 – TheDarkKnight Apr 12 '16 at 13:59
  • Oh that is going to be the problem. Which is just not realizeable at this point. could there be any other way to do this? Thanks – MuchWow Apr 12 '16 at 14:07
  • Qt 4.8 has [QDateTime::toTimeSpec](http://doc.qt.io/qt-4.8/qdatetime.html#toTimeSpec) which may be of use. – TheDarkKnight Apr 12 '16 at 16:19