如何在Mac OSX中获取实际的日历微秒时间(自1970年以来的时间)? [英] How to get the real calendar microseconds time (epoch since 1970) in Mac OSX?

查看:533
本文介绍了如何在Mac OSX中获取实际的日历微秒时间(自1970年以来的时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请仔细检查Qn以下内容:
为什么clang ++/g ++在Mac OSX中没有为chrono :: high_resolution_clock :: now()提供正确的微秒输出?

正如上面线程中已经讨论的那样,我打算从1970年开始获得microseconds时间. 现在,使用chrono::high_resolution_clock::now().time_since_epoch()可以在OSX&可能是iOS.在[我们的] Mac系统中,自系统重新启动以来生成微秒的时间.自1970年以来.

自1970年以来,是否有任何便携式(或Mac专用)方式来获取时间,精度为microseconds?
也欢迎使用针对Qt的解决方案.

解决方案

在macOS上,这是自1970-01-01 UTC(不包括leap秒)以来的毫秒数:

#include <chrono>
#include <iostream>

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

这只是对我的输出:

1503715928742714us

这不是很方便.尽管所有平台的system_clock都自1970年1月1日UTC(未指定,但实际上是标准)开始测量时间,但它们以不同的精度进行测量.仅macOS使用微秒.要输出微秒,请执行以下操作:

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

如果您想在探索其他平台这次测量的精度时执行此操作,则可以使用

对我来说,这只是输出:

1503716308206361µs

在gcc平台上,单位为ns.在Windows上,单位为[1/10000000]s,即1/10 µs或100ns.


关于chrono::high_resolution_clock:

该标准指定high_resolution_clock可以是system_clocksteady_clock的类型别名,也可以是单独的类型.在macOS和Windows上,high_resolution_clocksteady_clock的typedef.在gcc上,high_resolution_clocksystem_clock的typedef.因此,您不能依赖high_resolution_clock的类型或行为.我不建议使用它.


关于chrono::steady_clock:

steady_clock就像秒表.定时花多长时间非常好.但这无法告诉您一天中的时间.它与任何平台上的人类日历都没有任何关系.在macOS steady_clock上,自计算机启动以来以秒为单位.

有关当前std::chrono时钟不同的视频教程,请参阅 https://www.youtube.com/watch?v=P32hvk8b13M


system_clock计算自 Unix时代以来的时间(事实上​​的标准,不是官方的).正在考虑一项提案,以使其正式生效并添加以下其他时钟:

utc_clock :类似于system_clock,但包含leap秒.

tai_clock :测量自1958-01-01 00以来的物理秒数: 00:00.每次有a秒时,将在utc_clock之前移动一秒. 所有分钟均为60秒.

gps_clock :与tai_clock类似,但测量从第一个星期日开始的时间1980年1月的00:00:00 UTC.

file_clock :建议将其作为C + +17文件系统库基于.它没有指定,但是所有已知的实现都以某种方式将其与民用日历相关.

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.

解决方案

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.

这篇关于如何在Mac OSX中获取实际的日历微秒时间(自1970年以来的时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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