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. Qlocale::toDateTime() issue
Forum Updated to NodeBB v4.3 + New Features

Qlocale::toDateTime() issue

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.3k 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.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote on last edited by
    #1

    When a customer with a Spanish/Chile QLocale uses:

    QLocale().toDateTime( "03-09-2023 0:00:00", "dd-MM-yyyy H:mm:ss" );
    

    It returns an invalid QDateTime. This is because the clocks change in Chile on the first Sunday of September, so 03-09-2023 0:00:00 doesn't exist (see Stackoverflow ).

    But when I:

    QLocale::setDefault( QLocale( Spanish, Chile ) );
    QLocale().toDateTime( "03-09-2023 0:00:00", "dd-MM-yyyy H:mm:ss" );
    

    It returns a valid QDateTime. Why the difference?

    qDebug().noquote() << "Locale=" << QLocale().name();
    

    Returns:

    Locale= es_CL
    
    C 1 Reply Last reply
    0
    • AndyBriceA AndyBrice

      When a customer with a Spanish/Chile QLocale uses:

      QLocale().toDateTime( "03-09-2023 0:00:00", "dd-MM-yyyy H:mm:ss" );
      

      It returns an invalid QDateTime. This is because the clocks change in Chile on the first Sunday of September, so 03-09-2023 0:00:00 doesn't exist (see Stackoverflow ).

      But when I:

      QLocale::setDefault( QLocale( Spanish, Chile ) );
      QLocale().toDateTime( "03-09-2023 0:00:00", "dd-MM-yyyy H:mm:ss" );
      

      It returns a valid QDateTime. Why the difference?

      qDebug().noquote() << "Locale=" << QLocale().name();
      

      Returns:

      Locale= es_CL
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @AndyBrice What was the locale name reporting in the first example?

      AndyBriceA 1 Reply Last reply
      0
      • C ChrisW67

        @AndyBrice What was the locale name reporting in the first example?

        AndyBriceA Offline
        AndyBriceA Offline
        AndyBrice
        wrote on last edited by
        #3

        @ChrisW67 Also "es_CL" I believe.

        SGaistS 1 Reply Last reply
        0
        • AndyBriceA AndyBrice

          @ChrisW67 Also "es_CL" I believe.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AndyBrice even if it should, can you compare the two values to check if there's something amiss ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          AndyBriceA 1 Reply Last reply
          0
          • SGaistS SGaist

            @AndyBrice even if it should, can you compare the two values to check if there's something amiss ?

            AndyBriceA Offline
            AndyBriceA Offline
            AndyBrice
            wrote on last edited by
            #5

            @SGaist Is it possible that the determination of whether a datetime exists is based on the Windows locale, rather than the locale set by QLocale::setDefault() ?

            SGaistS 1 Reply Last reply
            0
            • AndyBriceA AndyBrice

              @SGaist Is it possible that the determination of whether a datetime exists is based on the Windows locale, rather than the locale set by QLocale::setDefault() ?

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              When using the default constructor, it uses the one returned by system hence it would be useful to check whether there's something different from it than when you set explicitly the locale.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              AndyBriceA 1 Reply Last reply
              0
              • SGaistS SGaist

                When using the default constructor, it uses the one returned by system hence it would be useful to check whether there's something different from it than when you set explicitly the locale.

                AndyBriceA Offline
                AndyBriceA Offline
                AndyBrice
                wrote on last edited by AndyBrice
                #7

                @SGaist I am explicitly setting the QLocale() at start-up from user preferences using:

                QLocale::setDefault( QLocale );

                I have set:

                QLocale::setDefault( QLocale( Spanish, Chile ) );

                And I believe the customer has as well. But Qlocale::toDateTime() is giving different time for him and me. I will look into sending him a test program to investigate further.

                AndyBriceA 1 Reply Last reply
                0
                • AndyBriceA AndyBrice

                  @SGaist I am explicitly setting the QLocale() at start-up from user preferences using:

                  QLocale::setDefault( QLocale );

                  I have set:

                  QLocale::setDefault( QLocale( Spanish, Chile ) );

                  And I believe the customer has as well. But Qlocale::toDateTime() is giving different time for him and me. I will look into sending him a test program to investigate further.

                  AndyBriceA Offline
                  AndyBriceA Offline
                  AndyBrice
                  wrote on last edited by
                  #8

                  @AndyBrice

                  If I run this:

                  #include <QCoreApplication>
                  #include <QLocale>
                  #include <QDateTime>
                  #include <QStringList>
                  #include <QtDebug>
                  #include <iostream>
                  
                  int main(int argc, char *argv[])
                  {
                      QCoreApplication a(argc, argv);
                  
                      QStringList dtl;
                      dtl << "03-09-2023 0:00:00";
                      dtl << "01-01-2023 0:00:00";
                      dtl << "04-09-2022 0:00:00";
                      dtl << "01-01-2022 0:00:00";
                      dtl << "05-09-2021 0:00:00";
                      dtl << "01-01-2021 0:00:00";
                  
                      QString df( "dd-MM-yyyy H:mm:ss" );
                  
                      qInfo() << "initial system locale=" << QLocale().name();
                      foreach ( const QString& dts, dtl )
                      {
                          QDateTime dt = QLocale().toDateTime( dts , df );
                          qInfo() << "dt.toString(" << dts << "," << df << "), dt.isValid()=" << dt.isValid() << " dt.toString()=" << dt.toString();
                      }
                  
                      qInfo() << "\nQLocale::setDefault( QLocale( QLocale::English, QLocale::UnitedKingdom ) )";
                      QLocale::setDefault( QLocale( QLocale::English, QLocale::UnitedKingdom ) );
                      qInfo() << "system locale=" << QLocale().name();
                      foreach ( const QString& dts, dtl )
                      {
                          QDateTime dt = QLocale().toDateTime( dts , df );
                          qInfo() << "dt.toString(" << dts << "," << df << "), dt.isValid()=" << dt.isValid() << " dt.toString()=" << dt.toString();
                      }
                  
                      qInfo() << "\nQLocale::setDefault( QLocale( QLocale::Spanish, QLocale::Chile ) )";
                      QLocale::setDefault( QLocale( QLocale::Spanish, QLocale::Chile ) );
                      qInfo() << "system locale=" << QLocale().name();
                      foreach ( const QString& dts, dtl )
                      {
                          QDateTime dt = QLocale().toDateTime( dts , df );
                          qInfo() << "dt.toString(" << dts << "," << df << "), dt.isValid()=" << dt.isValid() << " dt.toString()=" << dt.toString();
                      }
                  
                      return a.exec();
                  }
                  
                  

                  I get:
                  dcff2e82-d64f-4284-9094-8a354d1293eb-image.png

                  So the locale is having no effect on toDateTime(). But the customer is getting invalid QDateTime returned 0:00:00 on the first Sunday of each September (which is something to do with the Chilean calendar).

                  I tried changing the Windows region to Chile before running the program, but I still didn't see this date issue.

                  92d7ba7c-4f0e-40a9-8e6d-79291aa5e15a-image.png

                  Where is QLocale().toDateTime() picking up to use the Chilean calendar for him, but not for me?

                  AndyBriceA 1 Reply Last reply
                  0
                  • AndyBriceA AndyBrice

                    @AndyBrice

                    If I run this:

                    #include <QCoreApplication>
                    #include <QLocale>
                    #include <QDateTime>
                    #include <QStringList>
                    #include <QtDebug>
                    #include <iostream>
                    
                    int main(int argc, char *argv[])
                    {
                        QCoreApplication a(argc, argv);
                    
                        QStringList dtl;
                        dtl << "03-09-2023 0:00:00";
                        dtl << "01-01-2023 0:00:00";
                        dtl << "04-09-2022 0:00:00";
                        dtl << "01-01-2022 0:00:00";
                        dtl << "05-09-2021 0:00:00";
                        dtl << "01-01-2021 0:00:00";
                    
                        QString df( "dd-MM-yyyy H:mm:ss" );
                    
                        qInfo() << "initial system locale=" << QLocale().name();
                        foreach ( const QString& dts, dtl )
                        {
                            QDateTime dt = QLocale().toDateTime( dts , df );
                            qInfo() << "dt.toString(" << dts << "," << df << "), dt.isValid()=" << dt.isValid() << " dt.toString()=" << dt.toString();
                        }
                    
                        qInfo() << "\nQLocale::setDefault( QLocale( QLocale::English, QLocale::UnitedKingdom ) )";
                        QLocale::setDefault( QLocale( QLocale::English, QLocale::UnitedKingdom ) );
                        qInfo() << "system locale=" << QLocale().name();
                        foreach ( const QString& dts, dtl )
                        {
                            QDateTime dt = QLocale().toDateTime( dts , df );
                            qInfo() << "dt.toString(" << dts << "," << df << "), dt.isValid()=" << dt.isValid() << " dt.toString()=" << dt.toString();
                        }
                    
                        qInfo() << "\nQLocale::setDefault( QLocale( QLocale::Spanish, QLocale::Chile ) )";
                        QLocale::setDefault( QLocale( QLocale::Spanish, QLocale::Chile ) );
                        qInfo() << "system locale=" << QLocale().name();
                        foreach ( const QString& dts, dtl )
                        {
                            QDateTime dt = QLocale().toDateTime( dts , df );
                            qInfo() << "dt.toString(" << dts << "," << df << "), dt.isValid()=" << dt.isValid() << " dt.toString()=" << dt.toString();
                        }
                    
                        return a.exec();
                    }
                    
                    

                    I get:
                    dcff2e82-d64f-4284-9094-8a354d1293eb-image.png

                    So the locale is having no effect on toDateTime(). But the customer is getting invalid QDateTime returned 0:00:00 on the first Sunday of each September (which is something to do with the Chilean calendar).

                    I tried changing the Windows region to Chile before running the program, but I still didn't see this date issue.

                    92d7ba7c-4f0e-40a9-8e6d-79291aa5e15a-image.png

                    Where is QLocale().toDateTime() picking up to use the Chilean calendar for him, but not for me?

                    AndyBriceA Offline
                    AndyBriceA Offline
                    AndyBrice
                    wrote on last edited by
                    #9

                    @AndyBrice Could it be something to do with QTimeZone?

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • AndyBriceA AndyBrice

                      @AndyBrice Could it be something to do with QTimeZone?

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      It really seems to have something to do with the timezone: https://en.wikipedia.org/wiki/Time_in_Chile

                      Sadly I can't find when exactly they add/remove the one hour but according your observations it's between 0:00 and 1:00.
                      So I would play around with the timezones and also make sure that both of you have the same time zone information from your operating system - looks like chile switched a lot back and forth the last years...

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

                      AndyBriceA 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        It really seems to have something to do with the timezone: https://en.wikipedia.org/wiki/Time_in_Chile

                        Sadly I can't find when exactly they add/remove the one hour but according your observations it's between 0:00 and 1:00.
                        So I would play around with the timezones and also make sure that both of you have the same time zone information from your operating system - looks like chile switched a lot back and forth the last years...

                        AndyBriceA Offline
                        AndyBriceA Offline
                        AndyBrice
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher I wish I knew where this time zone was set in Windows though. Also whether there is any way to override it so Qlocale::toDateTime() gives the same result on different computers.

                        SGaistS 1 Reply Last reply
                        0
                        • AndyBriceA AndyBrice

                          @Christian-Ehrlicher I wish I knew where this time zone was set in Windows though. Also whether there is any way to override it so Qlocale::toDateTime() gives the same result on different computers.

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          I am also wondering whether there might be something with automatic (or missing) clock sync.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Christian EhrlicherC AndyBriceA 2 Replies Last reply
                          0
                          • SGaistS SGaist

                            I am also wondering whether there might be something with automatic (or missing) clock sync.

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            For your debugging program on the customers computer I would also output the return values of QTimeZone::systemTimeZone() and maybe other static functions from QTimeZone.

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

                            AndyBriceA 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              For your debugging program on the customers computer I would also output the return values of QTimeZone::systemTimeZone() and maybe other static functions from QTimeZone.

                              AndyBriceA Offline
                              AndyBriceA Offline
                              AndyBrice
                              wrote on last edited by
                              #14

                              @Christian-Ehrlicher Good idea. Just waiting for a response from the customer.

                              1 Reply Last reply
                              0
                              • SGaistS SGaist

                                I am also wondering whether there might be something with automatic (or missing) clock sync.

                                AndyBriceA Offline
                                AndyBriceA Offline
                                AndyBrice
                                wrote on last edited by
                                #15

                                @SGaist I finally managed to reproduce the issue by changing my Windows time zone to Santiago! So mystery solved.

                                0f3f2874-dbaf-4458-a071-e502736a6b99-image.png

                                Set to Santiago:

                                initial system locale= "en_GB"
                                QTimeZone::systemTimeZone()= QTimeZone("America/Santiago")
                                dt.toString( "03-09-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Jan 1 00:00:00 2023"
                                dt.toString( "04-09-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sat Jan 1 00:00:00 2022"
                                dt.toString( "05-09-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Fri Jan 1 00:00:00 2021"
                                
                                QLocale::setDefault( QLocale( QLocale::English, QLocale::UnitedKingdom ) )
                                system locale= "en_GB"
                                dt.toString( "03-09-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Jan 1 00:00:00 2023"
                                dt.toString( "04-09-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sat Jan 1 00:00:00 2022"
                                dt.toString( "05-09-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Fri Jan 1 00:00:00 2021"
                                
                                QLocale::setDefault( QLocale( QLocale::Spanish, QLocale::Chile ) )
                                system locale= "es_CL"
                                dt.toString( "03-09-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Jan 1 00:00:00 2023"
                                dt.toString( "04-09-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sat Jan 1 00:00:00 2022"
                                dt.toString( "05-09-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= false  dt.toString()= ""
                                dt.toString( "01-01-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Fri Jan 1 00:00:00 2021"
                                

                                Set to London:

                                initial system locale= "en_GB"
                                QTimeZone::systemTimeZone()= QTimeZone("Europe/London")
                                dt.toString( "03-09-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 3 00:00:00 2023"
                                dt.toString( "01-01-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Jan 1 00:00:00 2023"
                                dt.toString( "04-09-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 4 00:00:00 2022"
                                dt.toString( "01-01-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sat Jan 1 00:00:00 2022"
                                dt.toString( "05-09-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 5 00:00:00 2021"
                                dt.toString( "01-01-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Fri Jan 1 00:00:00 2021"
                                
                                QLocale::setDefault( QLocale( QLocale::English, QLocale::UnitedKingdom ) )
                                system locale= "en_GB"
                                dt.toString( "03-09-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 3 00:00:00 2023"
                                dt.toString( "01-01-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Jan 1 00:00:00 2023"
                                dt.toString( "04-09-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 4 00:00:00 2022"
                                dt.toString( "01-01-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sat Jan 1 00:00:00 2022"
                                dt.toString( "05-09-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 5 00:00:00 2021"
                                dt.toString( "01-01-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Fri Jan 1 00:00:00 2021"
                                
                                QLocale::setDefault( QLocale( QLocale::Spanish, QLocale::Chile ) )
                                system locale= "es_CL"
                                dt.toString( "03-09-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 3 00:00:00 2023"
                                dt.toString( "01-01-2023 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Jan 1 00:00:00 2023"
                                dt.toString( "04-09-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 4 00:00:00 2022"
                                dt.toString( "01-01-2022 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sat Jan 1 00:00:00 2022"
                                dt.toString( "05-09-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Sun Sep 5 00:00:00 2021"
                                dt.toString( "01-01-2021 0:00:00" , "dd-MM-yyyy H:mm:ss" ), dt.isValid()= true  dt.toString()= "Fri Jan 1 00:00:00 2021"
                                
                                1 Reply Last reply
                                1
                                • AndyBriceA AndyBrice has marked this topic as solved on

                                • Login

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