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.
source share