Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.


  • Moderators

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



  • @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.


  • Moderators

    @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



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



  • @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.


  • Moderators

    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)



  • @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();
    }


Log in to reply