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. QDateTime uses different time zone offset prior to UNIX epoch?
QtWS25 Last Chance

QDateTime uses different time zone offset prior to UNIX epoch?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 677 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.
  • P Offline
    P Offline
    patrickkidd
    wrote on last edited by
    #1

    QDateTime initializes with a different time zone offset when passed a QDate before versus after Jan 1 1970. The following line says it all:

    QDateTime(QDate(1969, 10, 14)).offsetFromUtc() != QDateTime(QDate(1970, 10, 14)).offsetFromUtc()

    It seems to me that the offsets for these two QDateTime objects should be equal. Otherwise, this makes initializing and storing dates difficult.

    I assume this is intentional. What is the rationale for this design decision? Is there a workaround?

    Cheers,
    -Patrick

    https://alaskafamilysystems.com/

    JKSHJ 1 Reply Last reply
    0
    • P Offline
      P Offline
      patrickkidd
      wrote on last edited by
      #7

      A reply from qt interest:


      Your test is not reliable. It works for less than half the planet and for just
      over half the year.

      Anyway, there's no workaround. This is done because the timezone databases are
      inaccurate before 1970. So we just return each timezone's standard time, not
      accounting for DST.

      This is documented:
      The range of valid dates taking DST into account is 1970-01-01 to the
      present, and rules are in place for handling DST correctly until 2037-12-31,
      but these could change. For dates falling outside that range, QDateTime
      makes a \e{best guess} using the rules for year 1970 or 2037, but we can't
      guarantee accuracy. This means QDateTime doesn't take into account changes
      in a time zone before 1970, even if the system's time zone database provides
      that information.

      https://alaskafamilysystems.com/

      1 Reply Last reply
      3
      • P patrickkidd

        QDateTime initializes with a different time zone offset when passed a QDate before versus after Jan 1 1970. The following line says it all:

        QDateTime(QDate(1969, 10, 14)).offsetFromUtc() != QDateTime(QDate(1970, 10, 14)).offsetFromUtc()

        It seems to me that the offsets for these two QDateTime objects should be equal. Otherwise, this makes initializing and storing dates difficult.

        I assume this is intentional. What is the rationale for this design decision? Is there a workaround?

        Cheers,
        -Patrick

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #2

        @patrickkidd said in QDateTime uses different time zone offset prior to UNIX epoch?:

        I assume this is intentional. What is the rationale for this design decision? Is there a workaround?

        Good question. I suggest you subscribe to the Interest mailing list and ask there. Qt engineers are active on that list, and they're probably in a better position to answer your question.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

          This looks some kind of (Windows?) bug to me:

          QDateTime date1(QDate(1969, 10, 14));
          QDateTime date2(QDate(1970, 10, 14));
          qDebug() << date1.offsetFromUtc() << date1.timeSpec() << date1.timeZone() << date1.isDaylightTime();
          qDebug() << date2.offsetFromUtc() << date2.timeSpec() << date2.timeZone() << date2.isDaylightTime();
          

          produces

          3600 Qt::TimeSpec(LocalTime) QTimeZone("Europe/Berlin") false
          7200 Qt::TimeSpec(LocalTime) QTimeZone("Europe/Berlin") true
          

          On Windows, Linux is correct - there was no DST in Germany in 1970.

          /edit: It's mktime which is wrong:

          tm local;
          memset(&local, 0, sizeof(local)); // tm_[wy]day plus any non-standard fields
          local.tm_mday = 14;
          local.tm_mon = 9;  // starts at 0
          local.tm_year = 70;
          local.tm_isdst = -1;
          

          returns local.tm_isdst = 1 which is wrong.
          I still would create a bug report with all the informations provided here. According to https://docs.microsoft.com/de-de/cpp/c-runtime-library/reference/mktime-mktime32-mktime64?view=vs-2019 the 'TZ' env var is maybe involved which could be set by Qt (but I'm not sure).

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

          kshegunovK 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            This looks some kind of (Windows?) bug to me:

            QDateTime date1(QDate(1969, 10, 14));
            QDateTime date2(QDate(1970, 10, 14));
            qDebug() << date1.offsetFromUtc() << date1.timeSpec() << date1.timeZone() << date1.isDaylightTime();
            qDebug() << date2.offsetFromUtc() << date2.timeSpec() << date2.timeZone() << date2.isDaylightTime();
            

            produces

            3600 Qt::TimeSpec(LocalTime) QTimeZone("Europe/Berlin") false
            7200 Qt::TimeSpec(LocalTime) QTimeZone("Europe/Berlin") true
            

            On Windows, Linux is correct - there was no DST in Germany in 1970.

            /edit: It's mktime which is wrong:

            tm local;
            memset(&local, 0, sizeof(local)); // tm_[wy]day plus any non-standard fields
            local.tm_mday = 14;
            local.tm_mon = 9;  // starts at 0
            local.tm_year = 70;
            local.tm_isdst = -1;
            

            returns local.tm_isdst = 1 which is wrong.
            I still would create a bug report with all the informations provided here. According to https://docs.microsoft.com/de-de/cpp/c-runtime-library/reference/mktime-mktime32-mktime64?view=vs-2019 the 'TZ' env var is maybe involved which could be set by Qt (but I'm not sure).

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #4

            @christian-ehrlicher said in QDateTime uses different time zone offset prior to UNIX epoch?:

            there was no DST in Germany in 1970

            That's really interesting. I wonder if Linux knows there was no DST in Germany after 1950, but there had been between 1939 and 1950 ...?
            I wonder if there's some table inside linux that resolves these historical cases ...

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @kshegunov said in QDateTime uses different time zone offset prior to UNIX epoch?:

              I wonder if there's some table inside linux that resolves these historical cases ...

              Isn't that what the tz database is for?

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

              kshegunovK 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @kshegunov said in QDateTime uses different time zone offset prior to UNIX epoch?:

                I wonder if there's some table inside linux that resolves these historical cases ...

                Isn't that what the tz database is for?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #6

                @christian-ehrlicher said in QDateTime uses different time zone offset prior to UNIX epoch?:

                Isn't that what the tz database is for?

                Not the faintest clue, hence my question. :)

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  patrickkidd
                  wrote on last edited by
                  #7

                  A reply from qt interest:


                  Your test is not reliable. It works for less than half the planet and for just
                  over half the year.

                  Anyway, there's no workaround. This is done because the timezone databases are
                  inaccurate before 1970. So we just return each timezone's standard time, not
                  accounting for DST.

                  This is documented:
                  The range of valid dates taking DST into account is 1970-01-01 to the
                  present, and rules are in place for handling DST correctly until 2037-12-31,
                  but these could change. For dates falling outside that range, QDateTime
                  makes a \e{best guess} using the rules for year 1970 or 2037, but we can't
                  guarantee accuracy. This means QDateTime doesn't take into account changes
                  in a time zone before 1970, even if the system's time zone database provides
                  that information.

                  https://alaskafamilysystems.com/

                  1 Reply Last reply
                  3

                  • Login

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