Difference between two time_point instances is not a duration?
It does produce a duration, but there are different kinds of durations. std::chrono::duration
is templatized on a representation type and a unit ratio. std::chrono::seconds
for example has a unit ratio of 1, while std::chono::nanoseconds
has a unit ratio of std::nano
, or 1/1000000000. time points have the same template parameters.
The specific unit ratio of std::chrono::system_clock::time_point
is implementation defined, but it is almost certainly less than than that of std::chrono::seconds
. As such, the duration produced from subtracting those two time points has much more precision than can be represented by std::chrono::seconds
. The default behaviour is to not allow assignments that lose precision with durations that have integer representations. So you can either use a duration with enough precision (std::chrono::system_clock::duration
) or cast the result to the duration you want (std::chrono::duration_cast<std::chrono::seconds>(...)
).
The difference between two time points is indeed a duration; but you can't implicitly convert one duration type to another, since that could silently lose precision.
If you want to reduce the precision from system_clock::duration
to seconds
, then you need to make the conversion explicit using a duration_cast
:
delay = duration_cast<std::chrono::seconds>(t1 - t2);
Alternatively, you might want to retain the precision of the system clock:
auto delay = t1 - t2; // Probably microseconds, or nanoseconds, or something
time_point - time_point
does return a duration
, just not the one in the code. You could replace std::chrono::seconds
with std::chrono::system_clock::duration
, or you could use a duration_cast
to convert to the kind you need.