PDA

View Full Version : QDateTime and Support for Daylight Savings Time



Goug
1st June 2011, 01:03
According to QtAssistant, the QDateTime class is aware of Daylight Savings Time, but there either seems to be some gaps, or I'm overlooking something.

The specific problem I have is that I need to converted formated date/time strings into a QDateTime; there's good support for that except (as far as I can see) when DST ends. A specific example is that, in 2010, in most of the U.S., DST ended on November 7, 2010 at 2:00 AM; the clock was set back an hour, producing two 2:00's and two hours between 1:00 and 2:00.

So if I have the string below to parse, it's impossible to determine, from the string alone, whether it's the first 1:15 AM on that day or the second one.


Nov 7, 2010 01:15

We keep a DST flag to identify these from when the formatted text was created (and no, I don't have the option of rewriting the system to pass UTC everywhere...sorry). Our existing date/time functions (that I'm trying to avoid because they're terrible) accept this flag and adjust the resulting UTC time accordingly.

Qt doesn't seem to provide any way of allowing me to specify which of those hours the string represents. I can check the flag and if set, add an hour to the result, but that will break down in locales that change time by some value other than an hour. Those are probably not relevant for my purposes at this time, but I hate to code something that I know is wrong.

Ideas?

Doug

wysota
1st June 2011, 01:21
I don't think Qt is in any way non-standard here. Even your own time specification doesn't have means to determine which of the zones the time represents and you need an additional flag to store this info. Only the UTC time contains such information so that should be your standard time specification. You can convert it to the local time (including timezones and DST) on demand using QDateTime::toLocalTime().