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

QSqlQuery (QPSQL) inserts QDateTime in UTC format but retrives it as local timezone

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3: Somewhat important
    • 6.8.0 FF
    • 5.15.3, 6.6.0 FF
    • None
    • OS: Ubuntu 22.04.2 LTS on Win;

      Database: PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compil ed by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

      OS Locale: pt_BR.utf8 (-3 hours from gmt)
    • All
    • b71f185ff (dev)

    Description

       

      I created a table using CREATE TABLE a ( data TIMESTAMP );. When inserting the curren datetime it inserts using the UTC timestamp, which means that if my local time is 12:00 the database will have 15:00 inserted.

      The issue is when retriving this time it will treat it as a local time, so it assumes my local time is 15:00 when it is 12:00;

      Below is a code to reproduce this issue. Note that the issue doesn't happen if the column type is TIMESTAMP WITH TIME ZONE, only when its TIMESTAMP.

      int main(int argc, char **argv) {
        // setting database
        QSqlDatabase db = QSqlDatabase::database();
        db = QSqlDatabase::addDatabase("QPSQL");
        db.setDatabaseName("xxx");
        db.setUserName("xxx");
        db.setPort(32768);
        db.setPassword("xxx");
        db.open();
      
        // inserting
        auto data = QDateTime::currentDateTime();
        QSqlQuery query;
        query.prepare("INSERT INTO a VALUES (?)");
        query.addBindValue(data);
        query.exec();
      
        // retriving
        QSqlQuery query2;
        query2.prepare("SELECT * FROM a");
        query2.exec();
        query2.next();
        auto data2 = query2.value(0).toDateTime();
      
        qDebug() << data;      // QDateTime(2023-08-09 10:33:47.878 -03 Qt::LocalTime)
        qDebug() << data2;     // QDateTime(2023-08-09 13:33:47.878 -03 Qt::LocalTime)
        assert(data == data2); // fails
      }
      
      

      Attachments

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

        Activity

          People

            chehrlic Christian Ehrlicher
            arthur_so Arthur Riuiti Pinheiro So
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes