Line 2 and 4 do print the same time, i.e they have the same time_t value which is always UTC, they are just formatted for different time zones.
QDateTime::fromTime_t() returns a QDateTime set to the local time corresponding to specified seconds after the epoch, i.e. it has Qt::LocalTime as its time spec, and this drives the string representation.
Try this:
qDebug
() <<
"Local:" <<
QDateTime::currentDateTime();
qDebug() << "UTC:" << utc;
uint time = utc.toTime_t();
result.setTime_t(time);
qDebug() << "Result:" << result;
qDebug() << "Local:" << QDateTime::currentDateTime();
QDateTime utc = QDateTime::currentDateTimeUtc();
qDebug() << "UTC:" << utc;
uint time = utc.toTime_t();
QDateTime result(QDate(), QTime(), Qt::UTC); // a null UTC time
result.setTime_t(time);
qDebug() << "Result:" << result;
To copy to clipboard, switch view to plain text mode
Local: QDateTime("Mon Feb 6 15:29:30 2012")
UTC: QDateTime("Mon Feb 6 05:29:30 2012")
Result: QDateTime("Mon Feb 6 05:29:30 2012")
To copy to clipboard, switch view to plain text mode
Bookmarks