views:

473

answers:

2

How can I convert between local and UTC time (in particular, from local to UTC) using boost::date_time using a current system timezone? I know about boost::date_time::local_adjustor, but it requires a template argument which is a timezone-dependent offset.

Failing platform-independent way to do that, how would I do it specifically on Linux?

As an aside, how are non-existent time points handled during the conversion? For example if a day is one hour short due to DST, and I try to convert a time point from the missing hour, what will be the resultant universal time?

+1  A: 

I'm using following code to find difference between local and UTC time:


    using namespace boost::posix_time;
    using namespace boost::gregorian;

    time_duration UTC_Diff;
    {
        ptime someUTC_Time(date(2008, Jan, 1), time_duration(0, 0, 0, 0));
        ptime someLocalTime = boost::date_time::c_local_adjustor::utc_to_local(someUTC_Time);
        UTC_Diff = someLocalTime - someUTC_Time;
    }

Since you find the difference is't easy to calcutate UTC time.

Dmitriy
It works, but unfortunately only one way, and I can't use the calculated offset since DST makes it different for different dates.
Alex B
A: 

Maybe this is of help

http://www.boost.org/doc/libs/1_42_0/doc/html/date_time/examples.html#date_time.examples.local_utc_conversion

celavek
Didn't pay attention to the whole question :( . The example in the doc is based on local_adjustor
celavek