[Qt-interest] QDateTime daylight saving parsing time offset
Konrad Rosenbaum
konrad at silmor.de
Wed Mar 30 17:02:07 CEST 2011
On Wed, March 30, 2011 14:52, Paul Drummond wrote:
> For a project I am working on I get a date time string in the following
> format:
>
> Tue, 29 Mar 2011 09:00:00 +0100
>
> Unless I'm mistaken (which happens a lot with me when working with
> date/time conversions!) the above string is in UTC format. When
> converted to a QDateTime the time should be 10:00:00, right? Or have
> I got that completely wrong?
It depends on whether you want the local time of where the string
originated or UTC.
If you want local time you just ignore the time zone spec (the "+0100").
It tells you just how far the time stamp in removed from UTC
If you want UTC you first get the local time and then _subtract_ the time
zone spec (which has the format +/-hhmm or +/-hh).
> I hate date/time hacking :-(
Me too. But long long gone are the times (how ironic) when earth was
flat... ;-)
> In Java you can specify the offset using the "z" format specifier but
> there is no format specifier for timezone in QDateTime unless I am
> mistaken?
QDateTime only understands UTC and local time and leaves all the
arithmatic to the operating system.
> I know QDateTime is supposed to handle daylight savings internally but
> I'm still not sure what to do with the +0100. Is it okay to just
> remove it from the string?
As said above: if you want local time only - yes.
If you are dealing with timestamps from many parts of the Internet and
actually need to compare them you need an implementation of time stamps
that can deal with this - Qt's can't, but if you can guarantee to have the
offset it is trivial to wrap something around QDateTime.
Little side hint: if the offset is missing from one of two timestamps you
have a 14-hour(*) uncertainty interval on each side in which you cannot
decide whether the time stamp is before or after the other one.
(*)some time zones go beyond 12.
Konrad
More information about the Qt-interest-old
mailing list