QDateTime::currentDateTime timezone issue ?
-
Hello,
I am currently have a real strange issue with QDateTime::currentDatetime that I cannot comprehend.
We have some code in Qt 5.12 we are porting to Qt 6.9.3. We are developping for Windows using MSVC2019.
The code uses
QDateTime::currentDatetime(). This worked fine in Qt 5.12 but in Qt 6.9.3 I have a 3 hour offset on my machine (this doesn't happen to my coworkers).I can fix this issue by sending the proper timezone to currentDateTime but when debugging it looks like currentDateTime already uses the right timezone:

I've been trying to understand what the issue could be, if there is anything I may have done wrong, but I have absolutely no clue of what is happening here.
Is this a known issue, have I made a mistake or is there something weird happening here ?
-
Looks like a bug, consider reporting it (but maybe first check with newest Qt version, perhaps it has already been fixed?).
-
Please use a recent Qt version. Also you forgot to create a Q(Core)Application object. This works fine for me with Qt 6.12 (git HEAD):
int main(int argc, char** argv) { QCoreApplication app(argc, argv); qDebug() << QDateTime::currentDateTime().timeZone(); qDebug() << QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm:ss"); qDebug() << QTimeZone::systemTimeZone(); qDebug() << QDateTime::currentDateTime(QTimeZone::systemTimeZone()).toString("dd/MM/yyyy hh:mm:ss"); return 0; } QTimeZone("Europe/Berlin") "25/09/2026 10:37:14" QTimeZone("Europe/Berlin") "25/09/2026 10:37:14" -
Hello,
I am currently have a real strange issue with QDateTime::currentDatetime that I cannot comprehend.
We have some code in Qt 5.12 we are porting to Qt 6.9.3. We are developping for Windows using MSVC2019.
The code uses
QDateTime::currentDatetime(). This worked fine in Qt 5.12 but in Qt 6.9.3 I have a 3 hour offset on my machine (this doesn't happen to my coworkers).I can fix this issue by sending the proper timezone to currentDateTime but when debugging it looks like currentDateTime already uses the right timezone:

I've been trying to understand what the issue could be, if there is anything I may have done wrong, but I have absolutely no clue of what is happening here.
Is this a known issue, have I made a mistake or is there something weird happening here ?
@f222
I have a quite different platform/environment from you: Qt 6.10.2 as supplied with Ubuntu 26.04, gcc compiler. I confirm your code works correctly for me --- perhaps not surprisingly since you say yours does not happen to coworkers.Btw, I thought initially the lack of
QCoreApplicationmight affect it, but I tried mine with and without and it made no difference (still correct). You might just put one in in case it affects your situation.From https://codebrowser.dev/qt6/qtbase/src/corelib/time/qdatetime.cpp.html#_ZN9QDateTime15currentDateTimeERK9QTimeZone the definition of QDateTime QDateTime::currentDateTime(const QTimeZone &zone) introduced at Qt 6.5 reads:
QDateTime QDateTime::currentDateTime(const QTimeZone &zone) { return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), zone); }while the overload without parameter is:
QDateTime QDateTime::currentDateTime() { return currentDateTime(QTimeZone::LocalTime); }Knowing this you might want to investigate a bit further why they are giving you different results in your environment and find where the underlying odd behaviour is coming from.