Getting the same value for timestamp on subsequent calls

I am trying to get the unix timestamp to milliseconds in a float when I call this function, which is probably 30 times / second. Timestamp returned by less always the same, when I type it up std::cerr, I get a different value. I create a variable every time I call a function, so I'm not sure what I'm doing wrong here. Any help would be appreciated.

    float GetUnixTimestamps()
    {
        float milliseconds_since_epoch =
        std::chrono::duration_cast<std::chrono::milliseconds>
        (std::chrono::system_clock::now().time_since_epoch()).count();

    std::cerr << milliseconds_since_epoch << std::endl;
    return milliseconds_since_epoch;
    }

EDIT: as per suggestion I am using C ++ 11 and should compile this on both Windows x64, x86, and Linux (mainly Ubuntu and CentOS). I would like to get the current time in unix. I already read the posts SO 19555121 and 16177295 , but the value still remains the same for the timestamp.

+4
source share
1 answer

floataccuracy is not good enough to do what you want here. The current number of milliseconds since 1970-01-01 is about 1,478,020,167,928. The next representable value in floatthereafter is 1,478,020,300,800 . This is approximately 131 seconds (2 minutes) later.

double will do here.

+5
source

Source: https://habr.com/ru/post/1659540/


All Articles