CLOCK_REALTIME有什么用? [英] What is the use of CLOCK_REALTIME?

查看:327
本文介绍了CLOCK_REALTIME有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 CLOCK_REALTIME CLOCK_MONOTONIC

CLOCK_REALTIME和CLOCK_MONOTONIC之间有区别吗?

CLOCK_REALTIME 时间不连续,可以向前和向后跳跃:这是时钟中的错误吗?时间不一致的时钟如何可靠?

The CLOCK_REALTIME has discontinuities in time, can jump forwards as well as backwards: is that a bug in this clock? How could a clock that gives inconsistent time be reliable?

推荐答案

尽管有缺陷,但CLOCK_REALTIME应该是系统对当前电流的最佳估计UTC或民事时间。这是系统显示相同时间的基础,无论您是看着手表,墙上的时钟,手机还是收听广播电台播放的时间,都可以看到相同的时间。显示的确涉及从UTC到当地时间的转换;稍后会详细介绍。)

Despite its imperfections, CLOCK_REALTIME should be the system's best estimate of the current UTC or civil time. It's the basis for the system's ability to display the same time you'd see if you looked at your watch, or a clock on the wall, or your cell phone, or listened to a time broadcast on a radio station, etc. (The display does involve a conversion from UTC to local time; more on this later.)

但是,如果CLOCK_REALTIME要匹配现实世界中的UTC时间,至少有两个非常重要的问题:

But if CLOCK_REALTIME is going to match the UTC time out there in the real world, there are at least two pretty significant issues:


  1. 如果有人不小心将计算机的时钟设置错了怎么办?他们将不得不对其进行修复,而该修复可能会涉及到时间跳变。几乎没有办法解决,特别是如果错误很大(例如,数小时或数天)的话。

  2. 不幸的是,大多数计算机都无法表示leap秒。因此,当在现实世界中有第二个飞跃时,大多数计算机时钟都必须跳一些。

所以当您阅读该书时, CLOCK_REALTIME可能有间断性,可能会向前和向后跳跃,这不是错误,它是一个功能:CLOCK_REALTIME 必须具有这些可能性,如果要应对现实世界时需要leap秒,偶尔会出错时钟。

So when you read that CLOCK_REALTIME might have discontinuities, might jump forwards as well as backwards, that's not a bug, it's a feature: CLOCK_REALTIME must have those possibilities, if it's to cope with the real world with leap seconds and occasionally-wrong clocks.

因此,如果您要编写的代码应能与现实世界中的时间相匹配,那么CLOCK_REALTIME就是您想要的,具有疣的东西。不过,理想情况下,如果偶尔由于某种原因使时钟向前或向后跳动,您将以一种合理的方式优雅地运行代码(不会崩溃或做一些奇怪的事情)。

So if you're writing code which is supposed to work with times matching those in the real world, CLOCK_REALTIME is what you want, warts and all. Ideally, though, you'll write your code in such a way that it behaves reasonably gracefully (does not crash or do something bizarre) if, once in a while, the clock jumps forward or backward for some reason.

您可能会从引用的其他问题中知道,CLOCK_MONOTONIC始终保证每秒精确地前进一秒钟,而不会出现跳跃或间断,但是时钟的绝对值意义不大。如果CLOCK_MONOTONIC的值为13:05,这并不意味着它是在下午一点之后,则通常意味着计算机已经启动并运行了13小时5分钟。

As you probably know from the other question you referenced, CLOCK_MONOTONIC is guaranteed to always step forward at exactly one second per second, with no jumps or discontinuities, but the absolute value of the clock doesn't mean much. If the CLOCK_MONOTONIC value is 13:05, that doesn't mean it's just after one in the afternoon, it typically means that the computer has been up and running for 13 hours and 5 minutes.

因此,如果您只关心相对时间,那么CLOCK_MONOTONIC可以。特别是,如果您想计时某件事花费了多长时间,则最好采用两个CLOCK_MONOTONIC值并将它们相减,因为如果存在某种时间跨度(这会影响CLOCK_REALTIME),这不会给您错误的答案。

So if all you're interested in is relative times, CLOCK_MONOTONIC is fine. In particular, if you want to time how long something took, taking two CLOCK_MONOTONIC values and subtracting them is preferable, since it won't give you a wrong answer if there was some kind of a time jump (that would have affected CLOCK_REALTIME) in between.

总而言之,正如人们在评论主题中所说的那样,CLOCK_REALTIME是绝对时间所需的,而CLOCK_MONOTONIC则是相对时间更好的。

Or, in summary, as people said in the comments thread, CLOCK_REALTIME is what you need for absolute time, while CLOCK_MONOTONIC is better for relative time.

现在,要多加一些点。

如前所述,CLOCK_REALTIME并不是很固定时间,因为它实际上可以处理在UTC。它使用自1970年以来著名的(臭名昭著?)Unix / Posix秒表示形式。例如,CLOCK_REALTIME值1457852399转换为2016年3月13日世界标准时间06:59:59。我住在格林威治以西五个小时的地方转换为当地时间01:59:59。但是一秒钟后,1457852400转换为03:00:00,因为夏时制开始了。

As mentioned, CLOCK_REALTIME is not quite "wall time", because it actually deals in UTC. It uses the famous (infamous?) Unix/Posix representation of seconds since 1970. For example, a CLOCK_REALTIME value of 1457852399 translates to 06:59:59 UTC on March 13, 2016. Where I live, five hours west of Greenwich, that translates to 01:59:59 local time. But one second later, 1457852400 translates to 03:00:00, because Daylight Saving Time kicked in.

我建议如果您的时钟不正确,则时间跳变是相当不错的这是解决它的唯一方法,但是事实并非如此。如果您的时钟仅略微偏离时钟,则可以通过逐渐调整时间(略微更改时钟频率)来进行校正,以便在几分钟或几小时后,它可以漂移到正确的时间而不会跳动。这就是NTP试图做的事情,尽管根据其配置,它可能只愿意为非常小的错误做这件事。

I suggested that if your clock was wrong, a time jump was pretty much the only way to fix it, but that's not quite true. If your clock is only slightly off, it's possible to correct it by "slewing" the time gradually (by changing the clock frequency slightly) so that after a few minutes or hours it will have drifted to the correct time without a jump. That's what NTP tries to do, although depending on its configuration it may only be willing to do that for errors that are pretty small.

我说过CLOCK_MONOTONIC通常是时候计算机已启动并正在运行。该标准并不能保证;所有标准说的是,CLOCK_MONOTONIC从某个任意时间点开始计时。在确实将CLOCK_MONOTONIC实现为系统启动时间的系统上,可以有两种解释:是启动后的时间,还是系统启动并运行的时间(也就是说,减去睡眠或挂起的时间) ?在许多系统上,还有另一个时钟CLOCK_BOOTTIME用来计时自启动以来(无论是启动还是挂起)的时间,而CLOCK_MONOTONIC仅计时系统启动和运行的时间。

I said that CLOCK_MONOTONIC was typically the time the computer has been up and running. That's not guaranteed by the standard; all the standard says is that CLOCK_MONOTONIC counts time since some arbitrary timepoint. On systems that do implement CLOCK_MONOTONIC as the time the system has been up, there can be two interpretations: is it time since boot, or the time the system has been up and running (that is, minus any time it was asleep or suspended)? On many systems, there's yet another clock CLOCK_BOOTTIME that counts time since boot (whether up or suspended), while CLOCK_MONOTONIC counts only time the system was up and running.

最后,如果您需要挂钟时间,但又想避免在leap秒时出现跳跃或不连续,则会遇到问题,因为在传统的Unix / Linux(以及Windows和所有其他)计算机系统中of秒的处理不善。在最近的(4.x?)Linux内核下,有一个CLOCK_TAI可能会有所帮助。一些实验系统可能会实现另一个时钟 CLOCK_UTC ,正确处理leap秒。两者都需要支付其他费用,并且至少在当今的支持水平下,您必须真正知道要有效使用它们的做法。有关更多信息,请参见 LEAPSECS邮件列表

Finally, if you want wallclock time, but you want to avoid jumps or discontinuities at leap seconds, you've got a problem, because of the poor handling of leap seconds in traditional Unix/Linux (and Windows, and all other) computer systems. Under recent (4.x?) Linux kernels, there's a CLOCK_TAI which may help. Some experimental systems may implement yet another clock, CLOCK_UTC, which handles leap seconds properly. Both of those have some other costs, and you'd have to really know what you were doing to use them effectively, at least with today's level of support. See the LEAPSECS mailing list for more information.

这篇关于CLOCK_REALTIME有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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