如何在Mac OSX中获取实际的日历微秒时间(自1970年以来的时间)? [英] How to get the real calendar microseconds time (epoch since 1970) in Mac OSX?
问题描述
请仔细检查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";
如果您想在探索其他平台这次测量的精度时执行此操作,则可以使用 对我来说,这只是输出: 在gcc平台上,单位为 关于 该标准指定 关于 有关当前 仅 Kindly go through below Qn for the context: As already discussed in above thread, I intend to get Is there any portable [or Mac specific] way to get the time since 1970, with the precision of On macOS this is how you can get microseconds since 1970-01-01 UTC (excluding leap seconds): This just output for me: This isn't quite portable. Though all platform's 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: For me this just output: On gcc platforms the units would be About The standard specifies that About For a video tutorial about the current different Only 这篇关于如何在Mac OSX中获取实际的日历微秒时间(自1970年以来的时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!1503716308206361µs
ns
.在Windows上,单位为[1/10000000]s
,即1/10 µs或100ns.
chrono::high_resolution_clock
:high_resolution_clock
可以是system_clock
或steady_clock
的类型别名,也可以是单独的类型.在macOS和Windows上,high_resolution_clock
是steady_clock
的typedef.在gcc上,high_resolution_clock
是system_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文件系统库基于.它没有指定,但是所有已知的实现都以某种方式将其与民用日历相关.
Why does clang++/g++ not giving correct microseconds output for chrono::high_resolution_clock::now() in Mac OSX?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.microseconds
?
Solution specific to Qt are also welcome.#include <chrono>
#include <iostream>
int
main()
{
std::cout << std::chrono::system_clock::now().time_since_epoch().count() << "us\n";
}
1503715928742714us
system_clock
s 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";
#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';
}
1503716308206361µs
ns
. And on Windows the units would be [1/10000000]s
, which is 1/10 of a µs or 100ns.
chrono::high_resolution_clock
: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.
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.std::chrono
clocks, please see https://www.youtube.com/watch?v=P32hvk8b13M
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.