3
#include<iostream>
#include<chrono>
using namespace std;

int main ()
{
  cout << "now = " << chrono::duration_cast<chrono::microseconds>
  (chrono::high_resolution_clock::now().time_since_epoch()).count() << "\n";
}

Above simple program compiled with clang++ (or g++) is returning wrong output in my Macbook Air Sierra as:

now = 28536840800

The same code runs fine in Ubuntu [also Windows, Android ...], with the proper output as:

now = 1503667687247859

Upon checking the standard library header file, the microseconds::rep size is 8 bytes (long long).
What is wrong with Mac machine?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Is your Mac set to February 1970? (just in case) – YSC Aug 25 '17 at 13:34
  • 1
    Are you sure mac is using unix time stamps? [This](https://stackoverflow.com/questions/6864798/apple-mac-and-unix-timestamps) makes it seem they use a different epoche. – nwp Aug 25 '17 at 13:34
  • @nwp, YSC, The date and time at the top right corner shows the current time only. How to check if the Mac machine I use, is set to the correct timestamp. This Mac is shared among the developers & testers. – iammilind Aug 25 '17 at 13:37
  • `date` on a terminal? But I was kind of joking and don't expect your machine to be set on 1970. – YSC Aug 25 '17 at 13:38
  • 1
    Generally, `time_since_epoch()` refers to the clock's epoch, which may or may not be the same as the Unix epoch. – Baum mit Augen Aug 25 '17 at 13:40
  • Why do you expect `high_resolution_clock`'s epoch to be the Unix one? – T.C. Aug 25 '17 at 13:41
  • So apparently, you booted that Mac about 8 hours before running that test. – Baum mit Augen Aug 25 '17 at 13:45
  • @BaummitAugen, thanks for linking the Qn. I tried a lot but couldn't find, may be it's due to the title of the Qn. However, my main problem is that I want the MacOSX (& iOS) to behave same as how Unix & others are behaving. Is there any way, by which I can get the real time since epoch in Mac? Possibly a platform independent way. Why there is no value to `time_since_epoch()`, shouldn't it act properly in Mac as well? – iammilind Aug 25 '17 at 13:52
  • @iammilind There is no standard way for sure, maybe some de-facto standard, but either way, that should be a new question. No one with the same problem would find some reply in this comment thread. :) – Baum mit Augen Aug 25 '17 at 13:58
  • @BaummitAugen, I have edited the Qn to make it objective & reopened. Feel free to answer. – iammilind Aug 25 '17 at 14:34
  • Hm, I don't think that's quite right yet. As the dupe explains, you are in fact getting a correct output. – Baum mit Augen Aug 25 '17 at 14:36
  • @T.C., actually that time will be used to store real date time for various events & matter. Please suggest alternatives if any. – iammilind Aug 25 '17 at 14:36
  • 1
    Tbh, I would have preferred it if you left this closed and asked your actual question as a new question. Now you removed a potentially valuable signpost, invalidated the entire comment thread, and still did not ask the right question. I think a rollback, reclose and then a new question would be best. – Baum mit Augen Aug 25 '17 at 14:37
  • 1
    I agree with the tree. Your edit changed this into a rather different question than it was initially. The new question should be posed as a new question, rather than added as a confusing afterthought into this one. – Cody Gray - on strike Aug 25 '17 at 18:46
  • 1
    @iammilind: The only portable clock is `system_clock`. The standard doesn't guarantee even this one, but it is de facto portable, and I am working on making it officially Unix Time. The other two clocks are only good for timing durations, and not for getting wall-clock time. For more info see this video tutorial for ``: https://www.youtube.com/watch?v=P32hvk8b13M – Howard Hinnant Aug 25 '17 at 19:15
  • @HowardHinnant sir, would you like to shed more light on this topic as an answer in this new post: [How to get the real calendar microseconds time (epoch since 1970) in Mac OSX?](https://stackoverflow.com/q/45891010/514235) – iammilind Aug 26 '17 at 01:01

0 Answers0