Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add milliseconds to the time duration since epoch?

Tags:

c++

time

chrono

Using only the std::chrono library, I have a variable with type unsigned long, representing the number of milliseconds from 1 Jan 1970 to now, and I want to add 100 milliseconds to it.

unsigned long now = std::chrono::system_clock::now().time_since_epoch().count();
unsigned long t100ms = std::chrono::milliseconds(100).count();
unsigned long time = now + t100ms;

When printing t100ms, I get "1000".

If I print the value of std::chrono::system_clock::now every second, I see that the incrementing number is not increased with 1000 at each iteration (as 1 second is equal to 1000 milliseconds, this should be the case).

Question

Does std::chrono::system_clock::now().time_since_epoch() not return the amount of time that has passed since 1 Jan 1970 represented in milliseconds?

like image 637
waas1919 Avatar asked Jul 23 '15 12:07

waas1919


3 Answers

std::chrono::system_clock::now().time_since_epoch() is the elapsed time since epoch, expressed in whatever units the system clock chooses to use; these could be attoseconds, or years, or anything in between. Clearly, adding a number of milliseconds to a number of nanoseconds (for example) is nonsensical.

If you want to perform arithmetic on duration values, you should remain in the duration types as they will perform unit conversion for you, and only call count() when you want to print out the result:

auto now = std::chrono::system_clock::now().time_since_epoch();
auto t100ms = std::chrono::milliseconds(100);
auto time = now + t100ms;
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
like image 137
ecatmur Avatar answered Nov 15 '22 00:11

ecatmur


count() member function of the duration class returns representation in periods, which is internal. It's not necessary will be seconds or milliseconds, it's just an internal representation. You can set the period manually for duration class, returned by time_since_epoch() function.

std::chrono::milliseconds foo = std::chrono::system_clock::now().time_since_epoch();

Now you have period value of the duration class is milliseconds.

like image 33
Ternvein Avatar answered Nov 14 '22 23:11

Ternvein


Beginning of time

std::chrono::time_point::time_since_epoch does not neccessarily return the time from 1 Jan 1970, instead the clock's epoch is implementation defined. Epoch is only meant to represent a fixed point in time that stays the same within an application session.

E.g., on my system std::chrono::time_point::time_since_epoch returns the time since latest system start.

Units

The date and time utilities in the std::chrono library are designed to work with unitless time amounts. This is to, e.g., enable adding two durations with different units as hours and nanoseconds. The unit conversions are performed automatically.

If you want to express time amounts with a specific unit, use std::chrono::duration_cast. Also to get the time-amount representation as a number use std::chrono::duration::count.

using namespace std::literals; // To enable duration literals as, e.g., 100ms.
auto time = std::chrono::steady_clock::now().time_since_epoch() + 100ms;
std::cout << "Ms: " << std::chrono::duration_cast<decltype(1ms)>(time).count();
like image 22
Felix Glas Avatar answered Nov 15 '22 00:11

Felix Glas