A Twisty Maze of Linux Clocks, All Slightly Different
geekwhisperer.blogspot.co.uk
geekwhisperer.blogspot.co.uk
At the very least, there are:
- the actual number of seconds that passed since the epoch, i.e. the number a perfect egg timer would show if you had set it off at 12 AM on 1970-01-01.
- International Atomic Time (IAT), the closest thing to the aforementioned egg timer we currently have (though with some offset, naturally).
- Terrestrial Time (TT), which is 32.odd seconds ahead of IAT, apparently for historical reasons.
- Universial Time (UT<timezoneoffset>), which is supposed to be the solar time at one particular location, Greenwhich for UT1.
- Coordinated Universal Time (UTC), probably the closest thing to The One True Time, which jumps around merrily depending on the rotational speed of Earth, trying to keep days of 86400 SI seconds.
Oh, and we’re not even considering relativistic effects here.
The ‘perfect egg timer’ is, if anything, comparable to the monotonic clocks.
> Given scheduling jitter and so forth, it's not clear what it means to get a timestamp with nanosecond resolution from the operating system. It probably takes thousands, if not millions, of instructions between the time the timestamp is retrieved and the time your program looks at it
Plus if it really does tick off in nanoseconds you'll get some random nanosecond number, which you can't use for scheduling, but will be good to feed to the PRNG.
The difference would be along the lines of lag vs precision. Measuring time in a non-hard-realtime operating system is a hairy proposition in the best of cases, after all you don't know how long your thread slept right after completing the system call but before your application got to do useful work. Usually this is only microseconds but it isn't rare at all to see this value have extremely high upper limits (100's of milliseconds or more...). This depends a lot on how many cores you have and the quality of the device drivers as well as which scheduler you run.
Or when NTP notices that your local clock is drifting, and attempts to discipline it.
As they say on the time-nuts listserv: A man with one clock always knows what time it is. A man with two clocks is never quite sure...
Even the brain has some neurons linked together that produces a kind of "clock", that is, a pulse that is circulary propagated via the participant neurons. For example the alpha wave. However, is not sure if this is linked to how we perceive time intervals.
Although, in one embedded program of mine, when I run it fast enough it spends more time inside of clock_gettime() measuring itself than it does actually running.
If you try to index a GPS trace of a car driving the quarter mile by the system clock, good luck, because you can experience astonishing precision without accuracy.
It gets especially interesting when you start thinking about mutexes and condition variables with timeouts.
But I notice that your solution is POSIX only. How about Windows?
And how do NTP adjustments affect this solution -- surely when the NTP server tells you your system clock is consistently off by 16%, there's got to be some effect on clock_gettime with CLOCK_MONOTONIC, right? I'd like to retain the ability to use NTP to improve the accuracy of the results.
UNIX time is not actually seconds since the epoch. It's just something very, very close. There are a few cases where the same time_t value repeats for two seconds in order to insert a leap second.
With regard to Windows, I assume there are equivalents to all these interfaces, but I don't know what they are.