Get the POSIX era as system_clock :: time_point

I know that the default value of std::chrono::system_clock::time_point is the era of the clock, but I cannot find any mandate in the C ++ 11 standard that the system_clock era is the same as the POSIX (1970) -01 era -01T00: 00: 00Z). Can we assume that in Linux and Windows it is? Or would it be wiser to use std::chrono::system_clock::from_time_t(0) ?

+6
source share
1 answer

The standard leaves an uncertain era std::chrono::system_clock::time_point .

There are three implementations of std::chrono::system_clock::time_point that I know of:

  • Lib ++
  • libstdc ++
  • VS

All three of them are thin wrappers around Unix Time , which counts the number of seconds that have passed since 00:00:00. Coordinated Universal Time (UTC), Thursday January 1, 1970, not counting the seconds of the jump.

All of them are based on a signed 64-bit integral type. None of them are stable. lib ++ has a tick period of microseconds. libstdC ++ has a tick period of nanoseconds, and VS has a tick period of 0.1 microseconds.

In case this is useful, here is an article that demonstrates some formula that allows you to use an indefinite but ordinary era to convert back and forth from a civil calendar without going through time_t .

The advantage of using std::chrono::system_clock::from_time_t is that it is guaranteed to work by standard. The disadvantage is that in practice it will limit you to the accuracy of the second (although this accuracy is not defined).

The advantage of the assumption that the era of std::chrono::system_clock is 1970-01-01, although it is not specified, is that you will be correct in all known implementations, and the accuracy available for this alternative is much higher. than the provided time_t .

+6
source

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


All Articles