Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PostgreSQL store QDateTime UTC

PostgreSQL store QDateTime UTC

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 825 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mk33
    wrote on 21 Jan 2020, 14:10 last edited by mk33
    #1

    Hello, I try to solve the problem by storing time in the database. I need time in the database in UTC. I use database type timestamp without time zone.

    I try to store two values (currentDateTime and curentDateTimeUtc) for local time 14:57.
    QDateTime(2020-01-21 14:57:17.770 Central Europe Standard Time Qt::TimeSpec(LocalTime))
    QDateTime(2020-01-21 13:57:17.775 UTC Qt::TimeSpec(UTC))

    	const QString queryText = "INSERT INTO dd_records (\"time\", substation, author, text, note, type)\n" \
    		"VALUES (:time, :substation, :author, :text, :note, :type);";
    
    	QSqlQuery query(database_);
    	query.prepare(queryText);
    	query.bindValue(":time",		rec.time_);
    	query.bindValue(":substation",	rec.subStation_);
    	query.bindValue(":author",		rec.author_);
    	query.bindValue(":text",		rec.event_);
    	query.bindValue(":note",		rec.note_);
    	query.bindValue(":type",		rec.type_);
    
            bool ok = query.exec();
    

    But both values are in the database with the same time
    "2020-01-21 14:57:17.775"
    "2020-01-21 14:57:17.77"

    What is a problem, I need time 13:57.

    PostgreSQL 11.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
    Qt 5.6.1 MSVC2010

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 21 Jan 2020, 14:41 last edited by
      #2

      can you try:
      query.bindValue(":time", rec.time_.toUtc().toString(Qt::ISODateWithMs));?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      M 1 Reply Last reply 21 Jan 2020, 15:20
      4
      • V VRonin
        21 Jan 2020, 14:41

        can you try:
        query.bindValue(":time", rec.time_.toUtc().toString(Qt::ISODateWithMs));?

        M Offline
        M Offline
        mk33
        wrote on 21 Jan 2020, 15:20 last edited by
        #3

        @VRonin said in PostgreSQL store QDateTime UTC:

        can you try:
        query.bindValue(":time", rec.time_.toUtc().toString(Qt::ISODateWithMs));?

        Thank you, problem solved.

        1 Reply Last reply
        1

        1/3

        21 Jan 2020, 14:10

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved