Important: Please read the Qt Code of Conduct -

[Solved] QDateTime to PostgreSQL

  • Hi guys,

    imagine the following:
    I've got a widget to enter a date, a time and an integer value to specify the offset from UTC in hours.
    The data set in this widget needs to be saved as a QDateTime and later inserted in a PostgreSQL database column of type "timestamp with timezone".

    What is the best way to construct this QDateTime object with the specified timezone and later construct an insert statement to be executed by the QSqlQuery? I find the topic of timezones and offsets and stuff pretty complicated and can't get my head around what information regarding the timezone is being saved by QDateTime.

    Maybe there's a recommended way for doing this. Any help is highly appreciated!


  • Lifetime Qt Champion


    You can build your QDateTime with this constructor so you be able to set the offset to the value your user wanted.

    And do the reverse when needed with offsetFromUtc

    Hope it helps

  • Thanks! But which TimeSpec do I pass into the constructor?

  • Hi,

    I suggest to use Qt::UTC when you store data.

    But if you want to use an offset from UTC you should use Qt::OffsetFromUTC with the specified offset

  • Thanks, that should do it!