Boost logo

Boost Users :

From: Andrew Schweitzer (a.schweitzer.grps_at_[hidden])
Date: 2006-08-22 22:36:29


gdprosch_at_[hidden] wrote:
> 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)…
>

Yes, all our stuff in in UTC.

> long epoch_in = 1140411002; // 2006-Feb-19 21:50:02
> ptime pt = from_epoch( epoch_in );
> long epoch_out = to_epoch( pt );
>
Did you find a solution to this?

In windows/MFC I did GetSystemTime, GetLocalTime, converted both to
CTimes, which can produce a UTC, subtracted the two, then subtracted the
  difference from whatever UTC system time was passed in.

>
> Many thanks,
> Greg Prosch
> Micron Technology, Inc.
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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