Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-37030

QDateTime::toString does not output timezone for Qt::ISODate format

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Do
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 5.2.1
    • Core: Date/Time
    • None
    • Windows 8

      QDateTime::toString is unable output the local system time in ISODate format with the timezone included.

      Using QDateTime::toTimeSpec(Qt::OffsetFromUTC) does not help because it converts the date to UTC using the local timezone offset.

      I can get the desired result using a work-around where I retrieve the offset minutes from the local time and then set the offset again, using the same value.

      The following SSCCE 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);
      
          // Work-around that gives the desired results
          QDateTime now = QDateTime::currentDateTime();
          int offset = now.offsetFromUtc();
          now.setOffsetFromUtc(offset);
          qDebug() << now.toString(Qt::ISODate);
      
          return 0;
      }
      

      This generates the following output:

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

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Eddy Edward Welbourne
            robbiee Robert Escott
            Votes:
            2 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes