[CPP] Parse Date/Time with timezone offset and convert to local time

Hi,

i would like to parse the following String “2020-10-14T21:22:24+02:00” to a QDateTime Object for the Timezone “Europe/Berlin” (or the Timezone of the Phone). The question how can i do this. I always seem to get the wrong local time. With Java (my primary language) this works fine and i get the expected result (-> “14.22.2020 21:10:24”) :

ZonedDateTime zdt = ZonedDateTime.parse(“2020-10-14T21:22:24+02:00”);
ZoneId swissZone = ZoneId.of(“Europe/Berlin”);
ZonedDateTime germanDate = zdt.withZoneSameInstant(swissZone);
LocalDateTime german = germanDate.toLocalDateTime();

However with Qt/CCP i do not get the same result - the offset always seems to be added
to the given time, not respecting my timezone:

QDateTime utcDateTime = QDateTime::fromString(utcDateTimeString, Qt::ISODate);
QDateTime localDateTime = QDateTime(utcDateTime.date(), utcDateTime.time(),
Qt::UTC).toLocalTime();

=> “2020-10-15 00:22:24”

i just don’t understand the Qt Documentation and i also could not find any decent examples.

Any help is appreciated.

Hi,
this is really intriguing me now. I am chasing an issue that I have since 1y+ and I just started working on it last week.
According that issue, I receive TZID=Vienna instead of Europe/Vienna and offset is incorrectly calcualted.

It seems there is something fishy around

try

QDateTime::toTimeSpec

utcDateTime variable is not really UTC. You parsed it from a date that is not in UTC. So either you convert it to UTC before using it, or you use it directly. From your code utcDateTime.time() is going to return 21:22:24, and then you tell Qt that this time is to be understood in UTC reference, so if your local time zone is UTC+03, then after converting it to local zone, you’ll end up to the day after.

In short, do this:

QDateTime dt = QDateTime::fromString(dtString, Qt::ISODate);
QDateTime localDateTime = dt.toLocalTime();

Hi,
for my issue it turned out it was in the synthesis library - so nothing to do with Sailfish.

1 Like