Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 chrono create time_point from number

Tags:

c++11

chrono

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?

like image 986
user3639812 Avatar asked Jul 23 '14 14:07

user3639812


People also ask

What is time_ point in c++?

> class time_point; (since C++11) Class template std::chrono::time_point represents a point in time. It is implemented as if it stores a value of type Duration indicating the time interval from the start of the Clock 's epoch. Clock must meet the requirements for Clock or be std::chrono::local_t (since C++20).

What is time_ since_ epoch in c++?

constexpr duration time_since_epoch() const; (since C++14) Returns a duration representing the amount of time between *this and the clock 's epoch.

What is Chrono number?

The Chronos Numbers are like a clock because Chronos means time so it will be time numbers and since there are twelve members (Number 13 is a special agent so wouldn't count with the rest of the numbers) like the face of a clock.

What is Chrono in C?

Chrono in C++ The unique thing about chrono is that it provides a precision-neutral concept by separating duration and point of time (“timepoint”) from specific clocks.


1 Answers

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;
like image 168
R. Martinho Fernandes Avatar answered Sep 30 '22 00:09

R. Martinho Fernandes