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::.toTimeZone("Africa/Cairo") gives wrong result.
Qt 6.11 is out! See what's new in the release blog

QDateTime::.toTimeZone("Africa/Cairo") gives wrong result.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 882 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.
  • S Offline
    S Offline
    Sayajirao
    wrote on last edited by
    #1

    I am using Qt5.8.0 in VC++ project. This project is supposed to work in all time zones. I installed the project on Windows 10 in "Africa/Cairo" time zone. I am calling APIs as below.
    QtimeZone clientTimeZone = QTimeZone(""Africa/Cairo");
    QDateTime dt = QDateTime::currentDateTime().toTimeZone(clientTimeZone);
    currecntDateTime(); gives correct output but after calling toTimeZone(clientTimeZone); date is modified by 2;00 hours which is wrong. For all other time zones the code is working fine. Please help me to resolve this issue.

    J.HilkJ 1 Reply Last reply
    0
    • S Sayajirao

      I am using Qt5.8.0 in VC++ project. This project is supposed to work in all time zones. I installed the project on Windows 10 in "Africa/Cairo" time zone. I am calling APIs as below.
      QtimeZone clientTimeZone = QTimeZone(""Africa/Cairo");
      QDateTime dt = QDateTime::currentDateTime().toTimeZone(clientTimeZone);
      currecntDateTime(); gives correct output but after calling toTimeZone(clientTimeZone); date is modified by 2;00 hours which is wrong. For all other time zones the code is working fine. Please help me to resolve this issue.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Sayajirao its not wrong, Cairo is indeed UTC +2


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      S 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        @Sayajirao its not wrong, Cairo is indeed UTC +2

        S Offline
        S Offline
        Sayajirao
        wrote on last edited by Sayajirao
        #3

        @J-Hilk Thanks a lot for your prompt reply.

        Yes, Cairo is UTC+2.
        When I call QDateTime::currentDateTime() in Cairo time zone, it gives the time according to that time zone. But when I call toTimeZone("Africa/Cairo");, it add 2 hours in the current time. This doesn't happen when the code is executed in "Harare/Pretoria" time zone or any other time zone.

        Suppose My code is running in Cairo timezone. Then the call to QDateTime::currentDateTime() gives me the current time (which is equal to the time shown in world clack). Assume it is 8:00 AM But when I call
        QDateTime dt = QDateTime::currentDateTime().toTimeZone(clientTimeZone); it gives the answer as 10:00 AM. This is wrong. This doesn't happen when I run the code in other time zone.

        J.HilkJ 1 Reply Last reply
        0
        • S Sayajirao

          @J-Hilk Thanks a lot for your prompt reply.

          Yes, Cairo is UTC+2.
          When I call QDateTime::currentDateTime() in Cairo time zone, it gives the time according to that time zone. But when I call toTimeZone("Africa/Cairo");, it add 2 hours in the current time. This doesn't happen when the code is executed in "Harare/Pretoria" time zone or any other time zone.

          Suppose My code is running in Cairo timezone. Then the call to QDateTime::currentDateTime() gives me the current time (which is equal to the time shown in world clack). Assume it is 8:00 AM But when I call
          QDateTime dt = QDateTime::currentDateTime().toTimeZone(clientTimeZone); it gives the answer as 10:00 AM. This is wrong. This doesn't happen when I run the code in other time zone.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Sayajirao mmh I don't quite follow!

          Could you make a quick compellable example?

          Should be possible in a few lines in main, I think :D


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          S 2 Replies Last reply
          0
          • J.HilkJ J.Hilk

            @Sayajirao mmh I don't quite follow!

            Could you make a quick compellable example?

            Should be possible in a few lines in main, I think :D

            S Offline
            S Offline
            Sayajirao
            wrote on last edited by
            #5

            @J-Hilk Please see my updated post before your reply.

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @Sayajirao mmh I don't quite follow!

              Could you make a quick compellable example?

              Should be possible in a few lines in main, I think :D

              S Offline
              S Offline
              Sayajirao
              wrote on last edited by
              #6

              @J-Hilk Here is the sample code.

              QTimeZone stz = QTimeZone::systemTimeZone();
              QDateTime::currentDateTime().toTimeZone(stz);

              Set the time zone of Windows 10 machine to "Africa/Cairo" and run the above code.
              The time you get will be 2 hours away from current time shown in World Clock.

              Now change the time zone to "Harare/Pretoria" and run the same code.
              The time you get is exactly equal to the time shown in World Clock. You will get the similar results for other time zones as well.

              1 Reply Last reply
              0
              • J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                So I made this example:

                int main(int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                
                    QTimeZone clientTimeZone1 = QTimeZone("Africa/Cairo");
                    QTimeZone clientTimeZone2 = QTimeZone("America/New_York");
                
                    QDateTime cdt = QDateTime::currentDateTime();
                    QDateTime cdtCiro = QDateTime::currentDateTime().toTimeZone(clientTimeZone1);
                    QDateTime cdtPretoria = QDateTime::currentDateTime().toTimeZone(clientTimeZone2);
                
                    qDebug() << cdt << endl << cdtCiro << endl << cdtPretoria;
                
                //    return app.exec();
                }
                
                

                with this output:

                QDateTime(2020-07-15 14:56:22.910 CEST Qt::LocalTime)
                QDateTime(2020-07-15 14:56:22.910 OEZ Qt::TimeZone Africa/Cairo )
                QDateTime(2020-07-15 08:56:22.910 GMT-4 Qt::TimeZone America/New_York )

                Seems to be working fine, (Qt5.12.6 btw)


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                S 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  So I made this example:

                  int main(int argc, char *argv[])
                  {
                      QApplication app(argc, argv);
                  
                      QTimeZone clientTimeZone1 = QTimeZone("Africa/Cairo");
                      QTimeZone clientTimeZone2 = QTimeZone("America/New_York");
                  
                      QDateTime cdt = QDateTime::currentDateTime();
                      QDateTime cdtCiro = QDateTime::currentDateTime().toTimeZone(clientTimeZone1);
                      QDateTime cdtPretoria = QDateTime::currentDateTime().toTimeZone(clientTimeZone2);
                  
                      qDebug() << cdt << endl << cdtCiro << endl << cdtPretoria;
                  
                  //    return app.exec();
                  }
                  
                  

                  with this output:

                  QDateTime(2020-07-15 14:56:22.910 CEST Qt::LocalTime)
                  QDateTime(2020-07-15 14:56:22.910 OEZ Qt::TimeZone Africa/Cairo )
                  QDateTime(2020-07-15 08:56:22.910 GMT-4 Qt::TimeZone America/New_York )

                  Seems to be working fine, (Qt5.12.6 btw)

                  S Offline
                  S Offline
                  Sayajirao
                  wrote on last edited by
                  #8

                  @J-Hilk Thanks a lot for your reply. Please run the below code by changing the timezone of the machine.

                  int main(int argc, char *argv[])
                  {
                  QApplication app(argc, argv);

                  QTimeZone  stz = QTimeZone::systemTimeZone();
                  
                  QDateTime cdt = QDateTime::currentDateTime();
                  QDateTime cdt1 = QDateTime::currentDateTime().toTimeZone(stz);
                  QDateTime cdt2 = QDateTime::currentDateTime().toTimeZone(stz).addMSecs(60000);
                  
                  qDebug() << cdt.toString(Qt::ISODateWithMs) << endl << cdt1.toString(Qt::ISODateWithMs) << endl << cdt2.toString(Qt::ISODateWithMs);
                  

                  // return app.exec();
                  }

                  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 Extensions
                  • Unsolved