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

QPSQL driver does not correctly parse datetime with time zone

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.4.1
    • 5.2.0, 5.3.2, 5.4.0 Beta
    • SQL Support
    • Linux, Windows, MacOs (All)
    • 190f64aab3fc8bb8e325bf48326c7b09d62b6419

    Description

      I have a PostgreSQL database table that contain timestamps in the format "2013-08-14 10:48:08.791993+09:30" but when querying the database the QDateTime returned is null. I've identified the offending Qt code in qtbase/src/sql/drivers/psql/qsql_psql.cpp and the fix is to turn off sanity checking. The code currently is:

      // remove the timezone
      // TODO: fix this when timestamp support comes into QDateTime
      if (dtval.at(dtval.length() - 3) == QLatin1Char('+') || dtval.at(dtval.length() - 3) == QLatin1Char('-'))
          dtval.chop(3);
      // milliseconds are sometimes returned with 2 digits only
      if (dtval.at(dtval.length() - 3).isPunct())
          dtval += QLatin1Char('0');
      if (dtval.isEmpty())
          return QVariant(QDateTime());
      else
          return QVariant(QDateTime::fromString(dtval, Qt::ISODate));

      Notice that the check to remove the timezone is incorrect (offset should be 6, not 3). The timezone is not removed and, because character len - 3 is punctuation, an extra '0' is appended, making the timestamp invalid.

      Now that Qt5.2 handles timezones this code could be removed. The fix that works for me is below:

      // remove the timezone
      // TODO: fix this when timestamp support comes into QDateTime
      //if (dtval.at(dtval.length() - 3) == QLatin1Char('+') || dtval.at(dtval.length() - 3) == QLatin1Char('-'))
      //    dtval.chop(3);
      // milliseconds are sometimes returned with 2 digits only
      //if (dtval.at(dtval.length() - 3).isPunct())
      //    dtval += QLatin1Char('0');
      //if (dtval.isEmpty())
      //    return QVariant(QDateTime());
      //else
          return QVariant(QDateTime::fromString(dtval, Qt::ISODate));

      Attachments

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

        Activity

          People

            israelins85 Israel Lins Albuquerque
            jedillama Adam Gray
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes