Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any simple way to get daylight saving time transition time under Linux in C/C++

Tags:

c++

c

linux

dst

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?

like image 836
Mysqto Avatar asked Jan 04 '13 22:01

Mysqto


People also ask

How do I set daylight savings in Linux?

As the hardware clock does not provide information as to whether it is kept in UTC or in local time, this needs to be configured explicitly, in YaST -> System -> /etc/sysconfig Editor -> System -> Environment -> Clock -> HWCLOCK.

How do I check my DST time?

Test transition of DST, i.e. when you are currently in summer time, select a time value from winter. Test boundary cases, such as a timezone that is UTC+12, with DST, making the local time UTC+13 in summer and even places that are UTC+13 in winter.

Is DST the real time?

Daylight Saving Time - When do we change our clocks? 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.


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.

like image 75
Mats Petersson Avatar answered Nov 02 '22 00:11

Mats Petersson


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.

like image 35
Csq Avatar answered Nov 02 '22 00:11

Csq