Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDateTime::currentDateTime timezone issue ?
Qt 6.11 is out! See what's new in the release blog

QDateTime::currentDateTime timezone issue ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 113 Views 1 Watching
  • 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.
  • F Offline
    F Offline
    f222
    wrote last edited by
    #1

    Hello,

    I am currently have a real strange issue with QDateTime::currentDatetime that I cannot comprehend.

    We have some code in Qt 5.12 we are porting to Qt 6.9.3. We are developping for Windows using MSVC2019.

    The code uses QDateTime::currentDatetime(). This worked fine in Qt 5.12 but in Qt 6.9.3 I have a 3 hour offset on my machine (this doesn't happen to my coworkers).

    I can fix this issue by sending the proper timezone to currentDateTime but when debugging it looks like currentDateTime already uses the right timezone:
    14eec021-6988-4712-82ec-c777bb2cf0c1-image.png

    I've been trying to understand what the issue could be, if there is anything I may have done wrong, but I have absolutely no clue of what is happening here.

    Is this a known issue, have I made a mistake or is there something weird happening here ?

    JonBJ 1 Reply Last reply
    1
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote last edited by
      #2

      Looks like a bug, consider reporting it (but maybe first check with newest Qt version, perhaps it has already been fixed?).

      (Z(:^

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote last edited by
        #3

        Please use a recent Qt version. Also you forgot to create a Q(Core)Application object. This works fine for me with Qt 6.12 (git HEAD):

        int main(int argc, char** argv)
        {
            QCoreApplication app(argc, argv);
            qDebug() << QDateTime::currentDateTime().timeZone();
            qDebug() << QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm:ss");
            qDebug() << QTimeZone::systemTimeZone();
            qDebug() << QDateTime::currentDateTime(QTimeZone::systemTimeZone()).toString("dd/MM/yyyy hh:mm:ss");
            return 0;
        }
        QTimeZone("Europe/Berlin")
        "25/09/2026 10:37:14"
        QTimeZone("Europe/Berlin")
        "25/09/2026 10:37:14"
        
        

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • F f222

          Hello,

          I am currently have a real strange issue with QDateTime::currentDatetime that I cannot comprehend.

          We have some code in Qt 5.12 we are porting to Qt 6.9.3. We are developping for Windows using MSVC2019.

          The code uses QDateTime::currentDatetime(). This worked fine in Qt 5.12 but in Qt 6.9.3 I have a 3 hour offset on my machine (this doesn't happen to my coworkers).

          I can fix this issue by sending the proper timezone to currentDateTime but when debugging it looks like currentDateTime already uses the right timezone:
          14eec021-6988-4712-82ec-c777bb2cf0c1-image.png

          I've been trying to understand what the issue could be, if there is anything I may have done wrong, but I have absolutely no clue of what is happening here.

          Is this a known issue, have I made a mistake or is there something weird happening here ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by JonB
          #4

          @f222
          I have a quite different platform/environment from you: Qt 6.10.2 as supplied with Ubuntu 26.04, gcc compiler. I confirm your code works correctly for me --- perhaps not surprisingly since you say yours does not happen to coworkers.

          Btw, I thought initially the lack of QCoreApplication might affect it, but I tried mine with and without and it made no difference (still correct). You might just put one in in case it affects your situation.

          From https://codebrowser.dev/qt6/qtbase/src/corelib/time/qdatetime.cpp.html#_ZN9QDateTime15currentDateTimeERK9QTimeZone the definition of QDateTime QDateTime::currentDateTime(const QTimeZone &zone) introduced at Qt 6.5 reads:

          QDateTime QDateTime::currentDateTime(const QTimeZone &zone)
          {
              return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), zone);
          }
          

          while the overload without parameter is:

          QDateTime QDateTime::currentDateTime()
          {
              return currentDateTime(QTimeZone::LocalTime);
          }
          

          Knowing this you might want to investigate a bit further why they are giving you different results in your environment and find where the underlying odd behaviour is coming from.

          1 Reply Last reply
          1
          • F Offline
            F Offline
            f222
            wrote last edited by
            #5

            I've been doing some more tests and its getting even weirder:

            • Added QCoreApplication instanciation
            • Updated Qt version (same result with Qt 6.11.2 and 6.12.0 rc)

            From what I see now:

            • Systemtime is correct UTC
            • LocalTime is my OS displayed time
            • When getting a currentDateTime from a timezone build with Qt::localTime the timezones seem to be different but I'm getting the same time
            • When getting a currentDateTime from a timeZone build with systemTimezone the timezones seem to be equal but I'm getting a different time

            I'm going to open a ticket but since the issue is only happening on my machine I think there must be something weird somewhere else 🤔

            image.png

            JonBJ 1 Reply Last reply
            0
            • F f222

              I've been doing some more tests and its getting even weirder:

              • Added QCoreApplication instanciation
              • Updated Qt version (same result with Qt 6.11.2 and 6.12.0 rc)

              From what I see now:

              • Systemtime is correct UTC
              • LocalTime is my OS displayed time
              • When getting a currentDateTime from a timezone build with Qt::localTime the timezones seem to be different but I'm getting the same time
              • When getting a currentDateTime from a timeZone build with systemTimezone the timezones seem to be equal but I'm getting a different time

              I'm going to open a ticket but since the issue is only happening on my machine I think there must be something weird somewhere else 🤔

              image.png

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by
              #6

              @f222
              Two very quick checks to eliminate, which may be irrelevant:

              • Does your machine/user have any TZ environment variable set?
              • Does your Registry have HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation and is it same as other users?
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved