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.
c ++ qt qtcore qdatetime qdate
RobbieE
source share