views:

327

answers:

2

Do you know if an API exists for that checking?

+2  A: 

GetTimeZoneInformation is what you need.

You can call it and inspect the returned value to detect whether daylight saving is on at the moment of call. It also fills a structure that contains rules for switching to daylight saving and from daylight saving. Having this structure filled and any given time in UTC format you can relatively easily compute whether that time corrspongs to daylight saving time or standard time.

sharptooth
is there any linux equivalent ? Or better a lib that encapsulate it ? I will check boost ;)
neuro
Be careful, though, if you want to take historical changes to transition dates into account. E.g. the US and Australia have recently changed the dates for when they alter between daylight saving time and normal time, and a given timestamp from 2003 lives under different rules than a given timestamp from 2008.
Kim Gräsman
Boost has Boost.Date_Time [1], containing a time zone database, among other things.I haven't used it, so I can't vouch for its quality.[1] http://www.boost.org/doc/libs/1_39_0/doc/html/date_time.html
Kim Gräsman
@Kim: Yes, that true and that's a problem that can't be easily solved.
sharptooth
thanks sharptooth
Nick D
@sharptooth: Newer Windows versions (Vista+) has a couple of helper functions for this, and Microsoft try to keep the registry updated with legislative changes. See GetTimeZoneInformationForYear [1] and GetDynamicTimeZoneInformation [2][1] http://msdn.microsoft.com/en-us/library/bb540851(VS.85).aspx[2] http://msdn.microsoft.com/en-us/library/ms724318(VS.85).aspx
Kim Gräsman
A: 

All well and good, but GetTimeZoneInformation and GetDynamicTimeZoneInformation only return the current time zone settings. What if the current TZ (i.e., where my server lives) is not the TZ that I want to check?

Let's say that I have a server app that reserves books for checkout. You can say "I want to check it out now" or "I'm going to need to check it out at datetime in the future". Checkout times are entered in the user's local time and converted to UTC before storing. When the user retrieves their list of checkouts the times are converted back to their local for display.

Assume that the server lives in New York and operates under standard post-2007 DST rules for the USA. The timezone is set to Eastern US, and it's currently 7/27/2009 15:30, so DST is ON.

Users in New York enter local dates and times. Convert from ET to UTC - not a problem. They enter a future date - fine. I use one of the above two API calls and figure it out.

However, a user in Sydney wants to reserve a checkout. She requests a checkout on 12/13/2009 18:25 relative to her local timezone in Sydney. I can't use my local TZ info - Sydney and NY don't follow the same DST rules. How do I go about loading Sydney's current TZ information and finding out if an arbitrary date is DST or not?