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.
Most of the United States begins Daylight Saving Time at 2:00 a.m. on the second Sunday in March and reverts to standard time on the first Sunday in November. In the U.S., each time zone switches at a different time. In the European Union, Summer Time begins and ends at 1:00 a.m. Universal Time (Greenwich Mean Time).
IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.
The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time.
We advance our clocks ahead one hour at the beginning of DST, and move them back one hour ("spring forward, fall back") when we return to standard time (ST). The transition from ST to DST has the effect of moving one hour of daylight from the morning to the evening.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With