Boost logo

Boost Users :

From: gdprosch_at_[hidden]
Date: 2006-03-09 16:08:27


I would like to convert epoch(long) time values to boost ptime and vice
versa. On the surface this seems relatively easy but some of the
functions needed are missing and there is an issue ensuring that the
local time is properly applied. Has someone come up with an easy way to
do this?

I can convert to ptime like this...

long epoch_in = 1140411002; // 2006-Feb-19 21:50:02
std::time_t stdtime ( epoch_in );
ptime = from_time_t( stdtime );

To convert from ptime back to epoch(long) there is no conversion
provided the other way. I found some code the author supplied a few
years ago here:
http://lists.boost.org/MailArchives/boost-users/msg07254.php
Unfortunately, the result does not correctly apply local time. The
following code...

std::time_t to_time_t(ptime t)
{
   ptime start(gregorian::date(1970,1,1),time_duration(0,0,0));
   return (t-start).total_seconds();
}

... produces a date with the value: 2006-Feb-20 04:50:02 and not
2006-Feb-19 21:50:02 (I am in US MST).

I've played around with applying the local offset but I end up with a
lot of lines of code that leave me wondering if there is a better way.

It would be nice if it were simple like this (assuming local time
adjustment is correctly applied)...

long epoch_in = 1140411002; // 2006-Feb-19 21:50:02
ptime pt = from_epoch( epoch_in );
long epoch_out = to_epoch( pt );

Many thanks,
> Greg Prosch
> Micron Technology, Inc.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net