16

Does anyone know if there is a good way to turn a boost::posix_time::ptime into an __int64 value. (I have compiled the microsecond version, not the nanosecond version).

I need to do this somehow as I am looking to store the resulting __int64 in a union type which uses the raw data for a high-performance application. Some type of Memento functionality like this would be highly useful for me. I'd like to avoid casts, if possible, but will resort to them if I need to.

Jacob
  • 34,255
  • 14
  • 110
  • 165
  • yep, that's what I've been thinking. The documentation keeps referring to every integral value as 'long'. Very annoying, especially since the example provided that shows how to calculate seconds since epoch uses the method that returns long's – Hassan Syed May 04 '10 at 16:37

4 Answers4

21

Converting a ptime to an integer is rather meaningless, since ptime is an abstraction of the actual time. An integer based time is a representation of that time as a count from an epoch. What you (probably) want to do is generate a time_duration from your time to the epoch you are interested in, then use the time_duration::ticks() to get the 64-bit integer. You may have to scale your result to your desired precision:

ptime myEpoch(date(1970,Jan,1)); // Or whatever your epocj is.
time_duration myTimeFromEpoch = myTime - myEpoch;
boost::int64_t myTimeAsInt = myTimeFromEpoch.ticks();
zdan
  • 28,667
  • 7
  • 60
  • 71
  • Ahah... I guess I had missed that I could subtract a time_duration. Thanks, this helps immensely. –  Oct 15 '09 at 20:43
2

from the docs: http://www.boost.org/doc/libs/1_40_0/doc/html/date_time/posix_time.html

boost::int64_t ticks() Return the raw count of the duration type (will give unpredictable results if calling time_duration is a special_value). time_duration td(0,0,0, 1000); td.ticks() // --> 1000

Zanson
  • 3,991
  • 25
  • 31
2
#include <boost/date_time/posix_time/posix_time.hpp>

// ...

uint64_t secondsSinceEpoch(const boost::posix_time::ptime& time) {
    namespace bpt = boost::posix_time;
    const bpt::ptime epoch = bpt::from_time_t(0);
    bpt::time_duration duration = time - epoch;
    return duration.total_seconds();
}

// ...

namespace bpt = boost::posix_time;
const bpt::ptime now = bpt::second_clock::local_time();
uint64_t seconds = secondsSinceEpoch(now);
std::cout << "seconds: " << seconds << std::endl;
neoneye
  • 50,398
  • 25
  • 166
  • 151
0

This is easy to do using the C++0x chrono classes, which are based on a simplified version of Boost's time/date library, but looking at the Boost docs I don't see anything particularly obvious. In fact the only method of doing this I can see is to call to_tm to convert it to a standard C struct tm and then multiply the fields together. This is kind of nasty, but should work (though you'll want to be careful about leap seconds and suchlike).

Jack Lloyd
  • 8,215
  • 2
  • 37
  • 47
  • 1
    Unfortunately, I also need to preserve the microsecond resolution, so this won't work. I see a from_ftime. Too bad there isn't a to_ftime. That would make matters easier. –  Oct 15 '09 at 20:05