Qt ISODate formatted date/time including timezone

19.8k views Asked by At

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.

3

There are 3 answers

6
László Papp On BEST ANSWER

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.

0
Rudolf Cardinal On

This seems to work, with millisecond accuracy and preservation of 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);
}
0
RDP On

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