Boost logo

Boost Users :

From: Jeff Garland (jeff_at_[hidden])
Date: 2006-05-04 08:31:05


Scott wrote:
> I noticed there's a from_time_t() helper function, but I need to convert
> from a ptime to a time_t. Is this the proper way to do it?
>
> inline
> std::time_t to_time_t(ptime t)
> {
> ptime start(gregorian::date(1970,1,1));
> time_duration dur = t - start;
> return dur.seconds();
> }
>
> I'm also curious why such functionality isn't already available?

I suspect that it's a plot by the date_time author to keep people from
using time_t ;-)

Seriously though, while your function will work for some times, there
are issues:

1) ptime can represent dates prior to 1970-1-1 and after 2038 (or
wherever the upper bound of time_t is). Probably need an exception for
these cases on the conversion.
2) ptime can represent times with less than 1 second resolution. Maybe
the function should round instead of truncating in case the time is
2006-May-04 00:00:00.999999?

So if you're sure your application doesn't care about any of these
issues than the function above should be fine.

Jeff


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