edA-qa mort-ora-y
edA-qa mort-ora-y

Reputation: 31951

Convert timestamp (in milliseconds) to boost ptime

I need to convert the a time measured in milliseconds since the epoch to a boost::posix_time::ptime. The only function I see is to convert is from_time_t but that is only in seconds and would lose the milliseconds.

How can I convert from milliseconds since epoch to a ptime type?

Upvotes: 4

Views: 7014

Answers (2)

ereOn
ereOn

Reputation: 55796

Where ms is your milliseconds count since the epoch:

ptime epoch_milliseconds_to_ptime(unsigned long int ms)
{
  static const ptime epoch(date(1970, 1, 1));

  return epoch + milliseconds(ms);
}

Upvotes: 13

Mike Seymour
Mike Seymour

Reputation: 254721

from_time_t(millis / 1000) + millisec(millis % 1000)

Upvotes: 9

Related Questions