Qlocale::toDateTime() issue
-
@AndyBrice even if it should, can you compare the two values to check if there's something amiss ?
-
@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() ?
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. -
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.@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.
-
@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.
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:
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.
Where is QLocale().toDateTime() picking up to use the Chilean calendar for him, but not for me?
-
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:
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.
Where is QLocale().toDateTime() picking up to use the Chilean calendar for him, but not for me?
@AndyBrice Could it be something to do with QTimeZone?
-
@AndyBrice Could it be something to do with QTimeZone?
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... -
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...@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.
-
@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.
I am also wondering whether there might be something with automatic (or missing) clock sync.
-
I am also wondering whether there might be something with automatic (or missing) clock sync.
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.
-
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.
@Christian-Ehrlicher Good idea. Just waiting for a response from the customer.
-
I am also wondering whether there might be something with automatic (or missing) clock sync.
@SGaist I finally managed to reproduce the issue by changing my Windows time zone to Santiago! So mystery solved.
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"
-
A AndyBrice has marked this topic as solved on