tags:

views:

368

answers:

2

I have the following time :

2010-01-25 03:13:34.384 - GMT Time Zone
2010-01-25 11:13:34.384 - My Local

I wish to convert to timestamp in ms. However, since I only obtain local time string from caller "2010-01-25 11:13:34.384"

If I do it this way :

// ts is "2010-01-25 11:13:34.384" (My Local)
boost::posix_time::ptime t(boost::posix_time::time_from_string(ts));
boost::posix_time::ptime end(boost::gregorian::date(1970,1,1));
boost::posix_time::time_duration dur = t - end;
// epoch is 1264418014384
// 2010-01-25 11:13:34.384 (GMT)  -- But I want 2010-01-25 03:13:34.384
// 2010-01-25 19:13:34.384 (My Local) -- But I want 2010-01-25 11:13:34.384
long long epoch = dur.total_milliseconds();

Is there any way to tell boost::posix_time, that the ts string which it receives, is belong to My Local timezone?

+2  A: 

I think you should be using boost::local_date_time, which handles time zones. There is an example in the documentation that is very similar to what you're trying to do: http://www.boost.org/doc/libs/1_41_0/doc/html/date_time/examples.html#date_time.examples.seconds_since_epoch

Emile Cormier
+1  A: 
Nikolai N Fetissov
May I know how do you obtain current time zone?
Yan Cheng CHEOK
You keep it in TZ environment variable.
Nikolai N Fetissov