Is there an easy way to get daylight saving time transition time on Linux in C / C ++

I want to get the transition time to DST on Linux with the provision of a time zone or TZ env. My path is stupid, giving the beginning of the year and checking every hour, checking the tm_isdst value of local time to get the transition time. Is there an easy way to do this?

+6
source share
2 answers

Glibc has source code that you can view here: http://sourceware.org/git/?p=glibc.git;a=tree;f=timezone

Or you can use the timezone database here: ftp://ftp.iana.org/tz/releases/tzdata2012c.tar.gz

Since you did not specify a specific time zone / location, I cannot find and provide you with accurate information.

+1
source

You can also use boost_datetime .

#include <iostream> #include <boost/date_time/local_time/local_time.hpp> using namespace boost::local_time; using namespace boost::posix_time; int main() { tz_database tz_db; tz_db.load_from_file("/path_to_boost/boost/libs/date_time/data/date_time_zonespec.csv"); time_zone_ptr zone = tz_db.time_zone_from_region("America/New_York"); ptime t1 = zone->dst_local_start_time(2013); ptime t2 = zone->dst_local_end_time(2013); std::cout << t1 << std::endl; std::cout << t2 << std::endl; } 

Some related SO links: C ++ How to find the time in another country based on daylight saving time? How to get the time zone (offset) for a location on a specific date?

But, as RedX said, politics can change time zones. Thus, in fact, your original solution has the advantage that it is automatically updated by the base OS. In addition, you can improve an existing solution using binary search.

+1
source

All Articles