将POSIX时代获取为system_clock :: time_point [英] Get POSIX epoch as system_clock::time_point

查看:147
本文介绍了将POSIX时代获取为system_clock :: time_point的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 std :: chrono :: system_clock :: time_point 的默认值是时钟的纪元,但是我在 system_clock 的纪元与POSIX纪元(1970-01-01T00:00:00Z)相同的C ++ 11标准。是否可以在Linux和Windows上假设是这种情况?还是使用 std :: chrono :: system_clock :: from_time_t(0)会更聪明?

I'm aware that the default value of a std::chrono::system_clock::time_point is the clock's epoch, but I can't find any mandate in the C++11 standard that system_clock's epoch is the same as the POSIX epoch (1970-01-01T00:00:00Z). Is it safe to assume on Linux and Windows that this is the case? Or would it be smarter to use std::chrono::system_clock::from_time_t(0)?

推荐答案

该标准未指定 std :: chrono :: system_clock :: time_point 的时代。

我知道以下 std :: chrono :: system_clock :: time_point 的三种实现:

There are three implementations of std::chrono::system_clock::time_point I am aware of:


  1. libc ++

  2. libstdc ++

  3. VS

所有这三个都是围绕 Unix时间的薄包装。计算自1970年1月1日星期四00:00:00协调世界时以来经过的秒数,不计算leap秒。

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

所有这些基于带符号的64位整数类型。他们都不是稳定的。 libc ++的滴答周期为微秒。 libstdc ++的滴答周期为纳秒,而VS的滴答周期为0.1微秒。

All of them are based on a signed 64 bit integral type. None of them are steady. libc++ has a tick period of microseconds. libstdc++ has a tick period of nanoseconds, and VS has a tick period of 0.1 microseconds.

如果有用,请这是一篇论文,该论文演示了一些公式,该公式可利用未指定但通用的纪元在不经过<$的情况下从民用日历来回转换c $ c> time_t 。

In case it is useful, here is a paper which demonstrates some formula to take advantage of the unspecified but common epoch to convert back and forth from the civil calendar without going through time_t.

使用 std :: chrono :: system_clock :: from_time_t 的优势code>可以保证按标准运行。缺点是在实践中会限制您精确到一秒(尽管该精度未指定)。

The advantage of using std::chrono::system_clock::from_time_t is that it is guaranteed to work by the standard. The disadvantage is that in practice it will limit you to the precision of a second (though that precision is unspecified).

假设纪元<$ c $的优势c> std :: chrono :: system_clock 是1970-01-01,尽管未指定,但在所有已知实现上您都是正确的,并且该替代方法可用的精度很高高于 time_t 提供的值。

The advantage of assuming the epoch of std::chrono::system_clock to be 1970-01-01, even though it is unspecified, is that you will be correct on all known implementations, and the precision available for this alternative is much higher than that provided by time_t.

更新

C ++ 2a规范草案现在指定 system_clock 来建模 Unix时间

The draft C++2a spec now specifies system_clock to model Unix Time:


26.7.1.1概述[time.clock.system.overview]

1类型为 system_clock 表示系统范围实时时钟的挂钟时间。类型 sys_time< Duration> 的对象测量自1970-01-01 00:00:00 UTC以来(及之前)的时间(不包括leap秒)。此度量通常称为 Unix时间。此措施有助于 sys_time 与日历类型(26.8)之间的有效映射。 [示例 sys_seconds {sys_days {1970y / January / 1}}。time_since_epoch() 0s sys_seconds {sys_days {2000y / January / 1}}。time_since_epoch() 946'684'800s 10'957 * 86'400s
最终示例]

1 Objects of type system_clock represent wall clock time from the system-wide realtime clock. Objects of type sys_time<Duration> measure time since (and before) 1970-01-01 00:00:00 UTC excluding leap seconds. This measure is commonly referred to as Unix time. This measure facilitates an efficient mapping between sys_time and calendar types (26.8). [Example: sys_seconds{sys_days{1970y/January/1}}.time_since_epoch() is 0s. sys_seconds{sys_days{2000y/January/1}}.time_since_epoch() is 946’684’800s, which is 10’957 * 86’400s. —end example]

摘要([time.syn])以前定义:

And the synopsis ([time.syn]) has previously defined:

template<class Duration>
  using sys_time  = time_point<system_clock, Duration>;
using sys_seconds = sys_time<seconds>;
using sys_days    = sys_time<days>;

我们希望 C ++ 2a中的 a为 0。

We are hopeful that the "a" in "C++2a" will be "0".

这篇关于将POSIX时代获取为system_clock :: time_point的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆