Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt ISODate formatted date/time including timezone

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

I should be able to just use the following:

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

but this always comes out in UTC format:

2014-02-24T01:29:00Z

Currently, the way I'm working round this is to force the TimeSpec to be Qt::offsetFromUtc by explicitly setting the offset, which I'm getting from the QDateTime originally.

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 must this be logged as a bug?

EDIT: I'm 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 one that is expected. Please note that the second time has been converted to UTC, which is not what is wanted.

like image 274
RobbieE Avatar asked Feb 23 '14 23:02

RobbieE


People also ask

How to display date and time in Qt?

QTime time = QTime::currentTime(); QString formattedTime = time. toString("hh:mm:ss"); QByteArray formattedTimeMsg = formattedTime. toLocal8Bit();

How to set current date in Qt?

The static function currentDate() creates a QDate object containing the date read from the system clock. An explicit date can also be set using setDate(). The fromString() function returns a QDate given a string and a date format which is used to interpret the date within the string.

How do I get system time in Qt?

Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, > Coordinated Universal Time (Qt::UTC). On systems that do not support time zones, this function will behave as if local time were Qt::UTC. See also setTime_t(). just tried pass string returned by QDateTime::currentDateTime().


2 Answers

This had not been present before 5.2, but it was integrated in there. It seems that you got the syntax incorrect though because it should be like this:

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

as per the corresponding bugreport. Note that toTimeSpec(Qt::OffsetFromUTC) call in the middle.

like image 104
lpapp Avatar answered Oct 12 '22 13:10

lpapp


When I need that 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 workaround has been tested on Qt 5.3

like image 28
RDP Avatar answered Oct 12 '22 11:10

RDP