Sometimes current time in milliseconds return minus value

Sometimes current time in milliseconds return minus value


We use below method to get current time in milliseconds (C++11 way).
So we were using this method from long time, and currently we came to know that in few device (occasionally, not all time) it return value in minus so it create problems.
Mostly we used this in Daily-chest etc
long curTimeMili = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();

We store long var when chest is set and do curTimeMili - pastTimeMili to check difference.

Anyone have deep knowledge into this? Can we just make value absolute (curTimeMili) and use it?

std::chrono::system_clock::now() returns system clock. It may change when user change it or e.g: there is summer/winter time switch.
Maybe you can use std::chrono::steady_clock?

Have you heard of Howard Hinnant? He is the defecto guy on std::time.

At one point he lived only a few miles from me :slight_smile: he works at Apple I think now and used to work at Metrowerks.

1 Like

That i know, but my problem is different.
Did you ever encounter minus value?

Hmm Nope. But i dont want to use any library for this.

long size can be 4 bytes which can be overflow.


Ohh yeah, that must be issue.
I dont know how i missed that.

Sure. Just to be clear he works on std::time he just puts out a library too for convenience of some developers.