Get Daylight Saving Transition Dates For Time Zones in C

帅比萌擦擦* 提交于 2019-11-30 09:18:09
vladr

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.

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.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!