tags:

views:

891

answers:

5

In C, is there a simple, cross-platform way of retrieving the dates that a given timezone begins and ends daylight saving?

I already have timezone offset information and whether or not daylight savings is currently being observed, but I really need the dates at which daylight savings begins and ends (for an external dependency I don't control). In Windows, I'm using GetTimeZoneInformation to get TimeZoneInfo, but I can't find a similar function for Linux/Solaris/Mac. I should point out also that I can't just rely on US rules for daylight savings adoption, as I can't predict the countries it will be used in.

The only way I'm aware of to get this information is via zdump or perhaps looking directly at the /usr/share/lib/zoneinfo files themselves, but I was hoping for a better solution.

Thanks for any help you can provide.

A: 

This is going to vary from system to system, so I don't think there is one single way you can do this across platforms.

Jon B
+1  A: 

I'm not sure there's a good cross-platform way to do this, but there is a programmatic interface to the /usr/share/zoneinfo files (or at least a description of the format) in tzfile.h, (available on Mac OS X and Linux).

Windows uses slightly different mappings from tzinfo, but there's a conversion table you could use to generify your interface between Windows and Unix.

tgamblin
+1  A: 

The portable and consistent way to deal with this is to build a year-round TZ offset table (for each day in the current year in the current timezone) at application startup. Resolve each day's midnight local time to UTC (GMT), and record the effective timezone offset vis-a-vis UTC (GMT) for the day in question. Look for transitions in the effective TZ offsets year-round (transitions will indicate DST changes.) This approach is consistent and portable in that only depends on localtime/gmtime functioning reliably on the platform in question, and will return results fully consistent with localtime/gmtime calls. However, it becomes a bit more complicated if you require DST information for other timezones (than the process' default timezone.)

You can also directly use the TZ/Olson database (code available for both *nix and Windows), for maximum flexibility, but unless the underlying runtime/OS use the exact same timezone information (and your application uses TZ/Olson timezone information and gmtime/localtime interchangeably) then you're in for unpleasant surprises.

Cheers, V.

vladr
A: 

In C, is there a simple, cross-platform way of retrieving the dates that a given timezone > begins and ends daylight saving?

No.

I implemented TZ support on VMS for DEC back in the late 90s.

Firstly, there is no cross-platform solution.

Secondly, the dates vary from year to year and also by legislative fiat. There is in fact no fully reliable programmatic solution.

Blank Xavier
A: 

If you need C++ (not C) look to boost::date_time library http://www.boost.org/doc/libs/1_38_0/doc/html/date_time.html

Dmitriy