Qt ISODate format date and time, including time zone

Does anyone know of a cleaner way to get the timezone included in the QDateTime ISO string representation?

I can simply use the following:

qDebug() << QDateTime::currentDateTime().toString(Qt::ISODate); 

but it always comes out in UTC format:

 2014-02-24T01:29:00Z 

Currently, the way I'm working on this is to force TimeSpec to be Qt :: offsetFromUtc, explicitly setting the offset that I get from QDateTime initially.

 QDateTime now = QDateTime::currentDateTime(); int offset = now.offsetFromUtc(); now.setOffsetFromUtc(offset); qDebug() << now.toString(Qt::ISODate); 

This gives what was originally expected:

 2014-02-24T01:29:00+02:00 

Does anyone know how to do this in a cleaner way or should it be logged as an error?

EDIT: I am using Qt5.2.1

UPDATE:

The following small program shows what I mean:

 #include <QtCore/QDateTime> #include <QtCore/QDebug> int main(int argc, int argv){ qDebug() << QDateTime::currentDateTime().toString(Qt::ISODate); qDebug() << QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate); QDateTime now = QDateTime::currentDateTime(); int offset = now.offsetFromUtc(); now.setOffsetFromUtc(offset); qDebug() << now.toString(Qt::ISODate); return 0; } 

The following output is generated:

 "2014-02-24T10:20:49" "2014-02-24T08:20:49Z" "2014-02-24T10:20:49+02:00" 

The last line is the expected one. Please note that the second time was converted to UTC, which is not required.

+8
c ++ qt qtcore qdatetime qdate
source share
3 answers

It was not until 5.2, but it was integrated there. It seems that you have the wrong syntax because it should be like this:

 QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate) 

according to the corresponding bugreport . Note that toTimeSpec (Qt :: OffsetFromUTC) is called in the middle.

+4
source share

When I need, I use the following workaround:

QDateTime::currentDateTime().toOffsetFromUtc(QDateTime::currentDateTime().offsetFromUtc()).toString(Qt::ISODate);

I have not tested if @lpappa is working on new versions. The above solution was tested on Qt 5.3

+2
source share

This seems to work with millisecond precision and saving timezone information:

 #include <QDebug> #include <QTimeZone> QString datetimeToIsoMs(const QDateTime& dt) { // An ISO-8601 format preserving millisecond accuracy and timezone. // Equivalent in moment.js: thing.format("YYYY-MM-DDTHH:mm:ss.SSSZ") // Example: '2016-06-02T10:04:03.588+01:00' // Here we also allow 'Z' for UTC. // In Qt, BEWARE: // dt; // QDateTime(2016-06-02 10:28:06.708 BST Qt::TimeSpec(LocalTime)) // dt.toString(Qt::ISODate); // "2016-06-02T10:28:06" -- DROPS timezone QString localtime = dt.toString("yyyy-MM-ddTHH:mm:ss.zzz"); int offsetFromUtcSec = dt.offsetFromUtc(); // FOR TESTING: offsetFromUtcSec = -(3600 * 2.5); QString tzinfo; if (offsetFromUtcSec == 0) { tzinfo = "Z"; } else { QString sign = offsetFromUtcSec < 0 ? "-" : "+"; offsetFromUtcSec = abs(offsetFromUtcSec); int hours = offsetFromUtcSec / 3600; int minutes = (offsetFromUtcSec % 3600) / 60; tzinfo += QString("%1%2:%3").arg(sign) .arg(hours, 2, 10, QChar('0')) .arg(minutes, 2, 10, QChar('0')); // http://stackoverflow.com/questions/2618414/convert-an-int-to-a-qstring-with-zero-padding-leading-zeroes } return localtime + tzinfo; } QString datetimeToIsoMsUtc(const QDateTime& dt) { QDateTime utc_dt = dt.toTimeSpec(Qt::UTC); return datetimeToIsoMs(utc_dt); } QDateTime isoToDateTime(const QString& iso) { return QDateTime::fromString(iso, Qt::ISODate); } 
+1
source share

All Articles