In which we synchronize computers
with the rest of the world.
PDF
Paperback
Files
Recap
- You can count the cycles of any regular phenomenon to keep time. Physics is very helpful here.
- .
- Unix time is a useful way to represent time in code. It is (roughly) the number of seconds since the beginning of 1970.
- .
- Python's time.time() checks the Unix time. Beware time zones and leap seconds.
- .
- time.monotonic() taps into an accurate monotonic clock that works beautifully for timing code.
- .
- time.sleep() is conservative tool for pausing your code. It never undersleeps, but always oversleeps just a bit.
- .
- These tools can be used to construct a pacemaker for keeping repetitive code executing on a strict cadence.
Resource links
-
1. Online Unix time converter:
https://www.epochconverter.com/ -
3. Python's time module:
https://docs.python.org/3/library/time.html -
4. List of UTC Offsets (Wikipedia):
https://en.wikipedia.org/wiki/List_of_UTC_offsets -
5. Sedat Kapanoğlu. Street Coder: The Rules to Break and How to Break Them.
Manning, 2021. ISBN:9781617298370
Website: streetcoder.org -
6. Aleksey Shipilёv. Nanotrusting the Nanotime. Last updated 2014-09-28.
https://shipilev.net/blog/2014/nanotrusting-nanotime/ -
7. From the Python 3.11 documentation for time.time():
https://docs.python.org/3.11/whatsnew/3.11.html#time
On Unix, time.sleep() now uses the clock_nanosleep() or nanosleep() function, if available, which has a resolution of 1 nanosecond (10-9 seconds), rather than using select() which has a resolution of 1 microsecond (10-6 seconds). (Contributed by Benjamin Szőke and Victor Stinner in bpo-21302.)
On Windows 8.1 and newer, time.sleep() now uses a waitable timer based on high-resolution timers which has a resolution of 100 nanoseconds (10-7 seconds). Previously, it had a resolution of 1 millisecond (10-3 seconds). (Contributed by Benjamin Szőke, Dong-hee Na, Eryk Sun and Victor Stinner in bpo-21302 and bpo-45429.)