0

could someone please explain why we need to subtract boost::gregorian::date(1970,1,1) from a boost::posix_time::ptime to obtain a POSIX notation of time (i.e. microseconds since 1970,1,1 midnight UTC time zone)

For example this link provides clear instructions, but not the reason:

How do I convert boost::posix_time::ptime to time_t?

Thanks

Community
  • 1
  • 1
Mannaggia
  • 4,559
  • 12
  • 34
  • 47
  • 1
    That link describes how to convert a _specific point in time_ to a _duration of time_. Are you familiar with the difference? – Drew Dormann Jun 12 '14 at 21:01
  • 1
    I think it's interesting that the `ptime` class includes a conversion from `time_t` that explicitly documents the involvement of the UNIX epoch: `ptime from_time_t(std::time_t)` - "Creates a ptime from the time_t parameter. The seconds held in the time_t are added to a time point of 1970-Jan-01", but there is no inverse function. I wonder why? – Michael Burr Jun 12 '14 at 21:23

1 Answers1

2

Because you are converting a date to a time.

From the Boost documentation :

Posix Time

Defines a non-adjusted time system with nano-second/micro-second resolution and stable calculation properties [...] This time system uses the Gregorian calendar to implement the date portion of the time representation.

So boost::posix_time::p_time is not just the epoch time (as return by time(), for example)), but rather the date and the time, expressed (obviously) since the beginning of the Gregorian calendar.

So if you want a Posix Epoch Time (from since 1970,1,1 midnight UTC time zone), you will need to do the subtraction of all those years.

quantdev
  • 23,517
  • 5
  • 55
  • 88