4

I am trying to get unix timestamp upto milliseconds in float whenever I call this function which is probably 30 times/second. The timestamp that the function below returns is always the same however, when I print it to std::cerr I get different value. I am creating the variable every time I call the function so I am not sure what I am doing wrong here. Any help will 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 the suggestion I am using C++ 11 and need to compile this on both Windows x64, x86 and Linux (mainly Ubuntu and CentOS). I would like to get the current time in unix. I have already read the S.O posts 19555121 and 16177295 but the value still remains the same for the timestamps.

Community
  • 1
  • 1
  • 2
    sounds like a compiler optimization. you need to post a [mcve], as well as state which compiler and which compilation flags you are using – m.s. Nov 01 '16 at 16:38
  • Define "is always the same". – Sam Varshavchik Nov 01 '16 at 16:57
  • For example first call I get 1478019383296 and for the second call to the same function I still get the same value which is 1478019383296. Converting Tue, 01 Nov 2016 16:56:23.296 GMT since the time is in milliseconds I expect to see a change in value based on the next call. – Umang Mukesh Mehta Nov 01 '16 at 17:00
  • @AndyG so how should I be sure that I get a different number. I need to store the timestamp in float and in millisecond. – Umang Mukesh Mehta Nov 01 '16 at 17:04
  • Could it be that you're calling it twice during the same millisecond? – Howard Hinnant Nov 01 '16 at 17:04
  • @HowardHinnant I am calling the same function atleast 30 times in a seconds, the value is same throughout. – Umang Mukesh Mehta Nov 01 '16 at 17:06
  • 1
    @UmangMukeshMehta: Try to place a `std::this_thread::sleep_for(std::chrono::milliseconds(33));` at the end of each call to `GetUnixTimestamps`... do you still get the same output? – AndyG Nov 01 '16 at 17:06
  • @UmangMukeshMehta: Simple code runs in microseconds or even nanoseconds. It's quite likely that you're polling the function too often for the millisecond to change. – AndyG Nov 01 '16 at 17:11
  • 3
    To those voting to close this question: Could you please stop closing questions before giving the OP time to clarify the question? It is not only irritating to the person asking the question, it is irritating to those trying to help him. – Howard Hinnant Nov 01 '16 at 17:16

1 Answers1

6

float's precision is not good enough to do what you want here. The current number of milliseconds since 1970-01-01 is about 1,478,020,169,728. The next representable value in a float after this is 1,478,020,300,800. That is about 131s (2min) later.

double will do here.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577