11

I am reading about the difference between CLOCK_REALTIME and CLOCK_MONOTONIC

Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?

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?

18
  • 2
    Daylight saving time is an example of wall clock time that may jump forwards and backwards. Also note that that answer clearly states it's the system's guess – and when it determines a previous guess was off, it guesses again.
    – Jongware
    Jan 27, 2017 at 12:42
  • 1
    Depends on what you need it for and what you propose to do with it, sometimes, it's useful to have a timestamp (think logging), or maybe you want to timestamp some objects (creation, completion of transaction etc.) Where it's useful to know the time of something. NOTE: this in no way implies suitability for performance measurement...
    – Nim
    Jan 27, 2017 at 12:46
  • 3
    @Bionix1441, that's the whole point, each clock type serves a different purpose, the monotonic clock is good for measuring elapsed time (think - how long did something take?), the realtime clock is useful as an indicator of time (think when did something happen?) The realtime clock (due to it's design) is unsuitable for measuring elapsed time (as it can go backwards and forwards.)
    – Nim
    Jan 27, 2017 at 12:58
  • 2
    @RadLexus Daylight savings time is not an example of something that could make CLOCK_REALTIME jump forwards or backwards, Jan 27, 2017 at 14:09
  • 3
    @RadLexus Sounds like you get this now, but for anyone else who is wondering: human-readable time jumps at DST changeovers, but CLOCK_REALTIME (like the Unix/Posix time it's based on) does not. CLOCK_REALTIME is UTC, without time zones or DST. It gets converted to human-readable time later, and that's where the time zones and DST offsets are introduced. Jan 27, 2017 at 14:33

1 Answer 1

26

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.)

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. What if someone accidentally sets the clock on your computer wrong? They're going to have to fix it, and the fix might involve a time jump. Pretty much no way around that, especially if the error is large (like, hours or days).
  2. Most computers unfortunately have no way of representing leap seconds. Therefore, when there's a leap second out in the real world, most computer clocks have to jump a little.

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.

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 system clock jumps forwards or backwards for some reason.

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.

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, by subtracting the start time from the end time, it's preferable to use CLOCK_MONOTONIC values for this, since they won't give you a wrong answer if there was some kind of a time jump (that would have affected CLOCK_REALTIME) in between.

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.

Now, a few more points.

As mentioned, CLOCK_REALTIME is not quite "wall time", because it actually deals in UTC. It uses the famous (infamous?) Unix/Posix representation of UTC 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 corresponds to 01:59:59 local time. But one second later was not 2:00 in the morning for me! In fact, 1457852399 + 1 = 1457852400 corresponds to 03:00:00 Eastern time, because that's when Daylight Saving Time kicked in here.

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.

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.

I said, "CLOCK_MONOTONIC is guaranteed to always step forward at exactly one second per second", but that may not be strictly correct, either. If your computer is in the middle of a time-slewing operation, trying to gradually correct an absolute time error, it may actually be the case that CLOCK_MONOTONIC is temporarily stepping at 1.001 seconds per second, or 0.999 seconds per second, or something like that. The discrepancy will usually be quite small, but in case it matters to you, some systems have yet other clock types you can use, such as CLOCK_MONOTONIC_RAW, that are supposed to be free of such perturbations.

Finally, if you want to track proper time, and 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 UTC time with leap seconds properly. Both of those have some other costs, though, 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.

7
  • 4
    Superb answer. Covers all the bases in an easy-to-digest manner. Jan 27, 2017 at 14:24
  • "That's what NTP tries to do, although it turns out it's willing to do that only for errors that are quite small (like, less than a second, I think)." That's entirely configurable in your NTP client Jan 27, 2017 at 15:08
  • @LightnessRacesinOrbit That depends on your NTP client, I think. I have it on good authority that the reference implementation will slew only if the error is less than 128 milliseconds, and there's no way to reconfigure this value. Jan 27, 2017 at 15:14
  • @LightnessRacesinOrbit But I think other ntp implementations are less fussy, so I tweaked the wording. Jan 27, 2017 at 15:37
  • 1
    @SteveSummit Awsome answer. Thank you
    – Bionix1441
    Jan 27, 2017 at 18:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.