tags:

views:

110

answers:

2

Hi,

in my c++ software I've used Boost in some parts and also for the local time. OK, now my problem is to make a check if in my machine is active or not the DST.

With the follow part of code I can know only the difference from the UTC time. In my case the difference is 2 hours because is active the DST

ptime tLoc = second_clock::local_time();
ptime tUTC = second_clock::universal_time();
time_duration tDiff = tUTC - tLoc;
local_time_zone = tDiff.hours();

I think that the boolean funcion has_dst() can help, right?

My system is Debian GNU/Linux.

Thanks

A: 

Look at plain C functions in time.h/ctime

localtime will return a struct tm*

struct tm has as its last field a flag telling if it is under DST or not.

Mark
Yes, with tm_isdst all work fine, but I can do this directly with Boost without use time.h?
spam2
A: 

I believe the function you are looking for is local_date_time_base<>::is_dst(). All date_time data types in Boost.DateTime are derived from local_date_time_base<>. The following should give you the required result:

namespace lt = boost::local_time;

// for example, use central time zone
lt::time_zone_ptr zone(new lt::posix_time_zone(
    "CST-06:00:00CDT+01:00:00,M3.2.0/02:00:00,M11.1.0/02:00:00"));
lt::local_date_time tloc = lt::local_sec_clock::local_time(zone);

std::cout << "I'm " << (tloc.is_dst() ? "" : "not ") << "in DST";
hkaiser
OK, but you must set the posix_time_zone. I don't know where the application will run. I can retrieve the timezone from the system with Boost?
spam2
If you call the `local_time()` function without any parameters: `tloc = lt::local_sec_clock::local_time();` it will return the current local time based on the local system settings.
hkaiser