QDateTime::fromString() returns invalid Datetime
-
This post is deleted!
@Christian-M
Firstly, I mistook you for the OP, I see now you were a responder. I was trying to answer/help the OP. I am not the one who is confused! I have been working with cross-timezone code for decades! What I am trying to discover is how to do something in Qt with this, which I have not done.My understanding of what the OP was asking is:
- He is in Australia/Melbourne, that is his local tz.
- He gets a datetime string to parse. It does not have a tz specification, e.g. something like
"2017-03-26T02:14:34.000"
. - He does know it represents a local time, and he does know which timezone it is from, but it is not his local timezone. And he does not know whether the foreign time lies in foreign DST or Standard time, he just knows the time is "correct" for a foreign local time. This is important.
- The question is what Qt code can he write to correctly parse that datetime from its "foreign" tz to a datetime in his local tz (or to UTC).
I do not see how your
QCalendar cal(QCalendar::System::Julian); QDateTime ts = QDateTime::fromString("02/10/2022 02:00:00", "dd/MM/yyyy hh:mm:ss", cal); //valid
can possibly address this. The fact that you use Julian calendar does not alter the problem. Since nowhere have you told it that the string comes from a foreign local time, it has no idea what this "02:00" time actually was in UTC.
Could you please show a worked example you claim which addresses this correctly? Let's say you are in Australia and you are given
"02/10/2022 02:00:00"
which you know to be in, say, UK time. But your code is not running in UK. Please show how you convert that to, say, UTC time in Qt. Thank you. -
@Christian-M
Firstly, I mistook you for the OP, I see now you were a responder. I was trying to answer/help the OP. I am not the one who is confused! I have been working with cross-timezone code for decades! What I am trying to discover is how to do something in Qt with this, which I have not done.My understanding of what the OP was asking is:
- He is in Australia/Melbourne, that is his local tz.
- He gets a datetime string to parse. It does not have a tz specification, e.g. something like
"2017-03-26T02:14:34.000"
. - He does know it represents a local time, and he does know which timezone it is from, but it is not his local timezone. And he does not know whether the foreign time lies in foreign DST or Standard time, he just knows the time is "correct" for a foreign local time. This is important.
- The question is what Qt code can he write to correctly parse that datetime from its "foreign" tz to a datetime in his local tz (or to UTC).
I do not see how your
QCalendar cal(QCalendar::System::Julian); QDateTime ts = QDateTime::fromString("02/10/2022 02:00:00", "dd/MM/yyyy hh:mm:ss", cal); //valid
can possibly address this. The fact that you use Julian calendar does not alter the problem. Since nowhere have you told it that the string comes from a foreign local time, it has no idea what this "02:00" time actually was in UTC.
Could you please show a worked example you claim which addresses this correctly? Let's say you are in Australia and you are given
"02/10/2022 02:00:00"
which you know to be in, say, UK time. But your code is not running in UK. Please show how you convert that to, say, UTC time in Qt. Thank you.This post is deleted! -
This post is deleted!
@Christian-M
Hi, it has taken me a day to get back to you.Unfortunately I am on Qt 5.12 where there is no
QCalendar
, that was introduced at 5.14. So I cannot test your whole principle of a default Julian calendar and passing that toQDateTime::fromString()
. So unless you know of an equivalent to your code for Qt <= 5.12 I cannot test anything.What I can say is that at 5.12
QDateTime foreingTz = QDateTime::fromString(datetime_foreign_with_no_timezone_spec); foreingTz.setTimeZone(QTimeZone(QByteArray(foreign_timezone)));
does seem to work, where my objective is to treat
datetime_foreign_with_no_timezone_spec
as a local time inforeign_timezone
. What it does is:- Initial
fromString()
treatsdatetime_foreign_with_no_timezone_spec
as a local time in my local timezone. - Calling
setTimeZone(foreign_timezone)
on that does seem to alter the interpretation of the original datetime string as a local time in the foreign timezone (which is what I am trying to achieve).
- Initial