user3639812
user3639812

Reputation: 103

C++11 chrono create time_point from number

I'm converting a std::chrono::time_point<std::chrono::high_resolution_clock> timestamp using

std::chrono::duration_cast<std::chrono::milliseconds>(
  getTimestamp().time_since_epoch()
).count()

to a 64 bit timestamp with millisecond precision. This is needed for some serialization in between of data. Later on I need to convert those timestamps back to a std::chrono::time_point<std::chrono::high_resolution_clock> for further processing. What is the proper way to do this in C++11?

Upvotes: 5

Views: 5581

Answers (1)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234354

Convert the number of milliseconds to a duration and add it to an epoch time_point:

auto epoch = std::chrono::time_point<std::chrono::high_resolution_clock>();
auto since_epoch = std::chrono::milliseconds(deserialised);
auto timestamp = epoch + since_epoch;

Upvotes: 12

Related Questions