8

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.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
user82116
  • 418
  • 5
  • 12
  • Have you find answer to your question? I need function similar to GetTimeZoneInformation() for Linux. – Dmitriy Aug 19 '09 at 01:02
  • I have precisely the same question about Java – morgancodes Sep 19 '09 at 20:33
  • No, I didn't find any usable solution as I need the solution to work in Solaris as well as Mac and Linux, however I think tgamblin's answer is probably the closest you would get to a workable solution. I haven't tried to do this in Java, so I can't help I'm afraid; I know that Java is supposed to use the Olson DB, but it doesn't seem to expose the DST date change info. – user82116 Sep 25 '09 at 11:29

4 Answers4

5

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.

vladr
  • 65,483
  • 18
  • 129
  • 130
2

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.

Todd Gamblin
  • 58,354
  • 15
  • 89
  • 96
  • 1
    tzfile.h does not describe much at all... It has a header, that's kind of it... I wonder why people write stuff like that. The time functions in libc must be able to read that data, why not make that public?! – Alexis Wilke Nov 21 '14 at 07:13
1

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.

0

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
  • 3,305
  • 7
  • 44
  • 55