2

Kindly go through below Qn for the context:
Why does clang++/g++ not giving correct microseconds output for chrono::high_resolution_clock::now() in Mac OSX?

As already discussed in above thread, I intend to get microseconds time since 1970.
Now using chrono::high_resolution_clock::now().time_since_epoch() works well in popular platforms except OSX & possibly iOS. In [our] Mac systems, the microseconds time is generated since the system restart & not since 1970.

Is there any portable [or Mac specific] way to get the time since 1970, with the precision of microseconds?
Solution specific to Qt are also welcome.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Does [`gettimeofday`](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/gettimeofday.2.html) not work correctly? – dbush Aug 26 '17 at 01:17
  • 3
    As there are currently 2 close votes on this question, I hereby post my intent to vote to reopen this question should it be closed. The current rationale for closing: Not clear what is being asked, is not true for me. It is perfectly clear to me what is being asked, and I have provided an answer. – Howard Hinnant Aug 26 '17 at 03:23
  • @dbush, I didn't use that function as it as part of "sys/" filesystem, which may not be portable in Windows. Besides, I wanted to use the C++11 facility. – iammilind Aug 26 '17 at 04:59
  • 1
    Of the three standard clocks, only `system_clock` is guaranteed to give you wall-clock time. `steady_clock` is "[not related to wall clock time](http://en.cppreference.com/w/cpp/chrono/steady_clock)," and `high_resolution_clock` "[may be an alias of ... `steady_clock`](http://en.cppreference.com/w/cpp/chrono/high_resolution_clock)." – Tavian Barnes Oct 23 '17 at 23:21

1 Answers1

5

On macOS this is how you can get microseconds since 1970-01-01 UTC (excluding leap seconds):

#include <chrono>
#include <iostream>

int
main()
{
    std::cout << std::chrono::system_clock::now().time_since_epoch().count() << "us\n";
}

This just output for me:

1503715928742714us

This isn't quite portable. Though all platform's system_clocks do measure time since 1970-01-01 UTC (not specified but de facto standard), they do so with differing precisions. Only macOS uses microseconds. To portably output microseconds:

using namespace std::chrono;
std::cout << time_point_cast<microseconds>(system_clock::now()).time_since_epoch().count() << "us\n";

If you would like to do this operation while exploring what precision other platforms measure this time with, you can use Howard Hinnant's date/time library to very easily do this:

#include "date.h"
#include <chrono>
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    std::cout << system_clock::now().time_since_epoch() << '\n';
}

For me this just output:

1503716308206361µs

On gcc platforms the units would be ns. And on Windows the units would be [1/10000000]s, which is 1/10 of a µs or 100ns.


About chrono::high_resolution_clock:

The standard specifies that high_resolution_clock may be a type alias for system_clock or steady_clock, or it may be a separate type. On macOS and Windows, high_resolution_clock is a typedef for steady_clock. On gcc high_resolution_clock is a typedef for system_clock. So you can't depend on the type or behavior of high_resolution_clock. I don't recommend its use.


About chrono::steady_clock:

steady_clock is like a stopwatch. It is great for timing how long something takes. But it can't tell you the time of day. It has no relationship whatsoever to a human calendar on any platform. On macOS steady_clock it is a count of nanoseconds since the computer booted.

For a video tutorial about the current different std::chrono clocks, please see https://www.youtube.com/watch?v=P32hvk8b13M


Only system_clock counts time since the Unix epoch (de facto standard, not official). There is a proposal under consideration to make this official, and to add these additional clocks:

utc_clock: Like system_clock but includes leap seconds.

tai_clock: Measures physical seconds since 1958-01-01 00:00:00. Moves a second ahead of utc_clock every time there is a leap second. All minutes have 60 seconds.

gps_clock: Like tai_clock but measures time since the first Sunday of January 1980 00:00:00 UTC.

file_clock: This is proposed to be the clock that the C++17 filesystem library is based on. It isn't specified, but all known implementations relate this somehow to the civil calendar.

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