C++11 std::chrono subtract now and min
c++, c++-chrono
Solution
As has been pointed out this is the result of overflow. Remember that the minimum value a signed type can represent is about the same magnitude as the largest. If `now` is positive then the difference between `now` and `min` will clearly have greater magnitude than `min`, which means it has greater magnitude than the type's largest value can represent.
If you want to guarantee a positive duration then instead of using the minimum you could instead use a steady clock and then use the program start time as the base. The built-in clock durations are all specified such that a duration should be able to represent a range of at least a couple hundred years, so unless your program runs for longer than that you'll get positive durations.
Another option is to choose a clock where the epoch is known to be in the past and simply say
Clock::now().time_since_epoch();
Problem
I feel like I'm going a little crazy with this one, but it just doesn't make sense to me. In my mind, if I subtract the minimum time point from any time point returned from a `now()` call, I should always get a positive duration, but that doesn't happen. ``` #include <chrono> #include <iostream> typedef std::chrono::steady_clock myclock; int main(int argc, char **argv) { myclock::time_point min = myclock::time_point::min(); myclock::time_point now = myclock::now(); auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now - min).count(); std::cout << millis << std::endl; } ``` Why does this print a negative integer and not a positive integer? (clang 3.3 or g++ 4.8.1)