I want to get the transition time for DST Under Linux with giving time zone or TZ env. My way is stupid, giving the start of the year and try every hour then check tm_isdst value of local time to get the transition time. Is there some simple way to do this?
-
Beware that whatever you use you will need to update the database often, as timezones are purely a political thing. – RedX Jan 04 '13 at 23:51
-
4Having either "time zone" or "daylight saving" in one sentence with "simple" is not applicable. Whichever people decided on these things were complete idiots and ignorant of the fact that anyone might possibly have to deal with the crap they produced later. The by far most moronic thing done with time zones not long ago was changing the time zone of Caracas by _half an hour_ for the sole reason that Mr. Chavez thought it was not "cool enough" if Venezuela had the same time zone as some other country. Now don't get me started on DST in geographically similar countries switching weeks apart... – Damon Jan 05 '13 at 01:38
-
You could improve your search by binary chop; evaluate the offset on 1st January, 1st July, 31st December. If they're the same, there's a good chance there is no change in the time zone. If the zone in the middle is different, you can then do a binary chop to see whether the first switch is before or after 1st April; then, based on that result, chop the relevant section in half again. This should be quite a lot quicker. Time zones normally switch on the hour, so you can do your refinement by months, then days, then hours. Repeat for the second half of the year. This should be a *lot* faster. – Jonathan Leffler Jan 12 '13 at 03:58
2 Answers
There is the source code in glibc, which you can browse 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 haven't given a particular timezone/location, I can't look up and give you the exact information for you.

- 126,704
- 14
- 140
- 227
-
+1. `zdump -v America/Los_Angeles` (e.g.) does something interesting, so the source should be relevant. That said, there appears to be no standard API to answer the question the OP is asking. – Nemo Jan 12 '13 at 05:27
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 foreign country taking into account daylight saving? How do you get the timezone (offset) for a location on a particular date?
But as RedX said earlier, politics may change time zones. So actually your original solution has the advantage of being automatically updated with the underlying OS. Also, you can improve your existing solution by using binary search.