education-2022/selected/time.md

277 lines
18 KiB
Markdown
Raw Normal View History

2022-05-22 02:39:17 +00:00
# Time
Time is a very deceptive topic for programmers. "I know how time works", they all think. "After all, I use a calendar and clock every day!" Unfortunately, human civilizations have been measuring time for a long time<sup>[citation needed]</sup>, and even our modern systems carry centuries of historical baggage. This article will attempt to give you a broad working knowledge of time, and how it pertains to the computer systems you will work on in your lifetime.
There are two major (and largely separate) concerns related to time: *when things happen* (instants in time), and *how long things take* (durations). We will cover each of them in turn.
No matter how confused you may become as you read this article, never forget that time always moves forward at the same rate everywhere. Nothing you do (switching timezones, changing clocks, etc.) can change this fact, and any confusion is simply the result of human notations for time. Unless you have to deal with relativity—in that case, god help you.
2022-05-30 03:30:00 +00:00
# When things happen: instants in time
2022-05-22 02:39:17 +00:00
The first major aspect of time for a programmer is the study of *when things happen*. This is the realm of calendars and clocks.
A first important distinction is the difference between *dates* and *times*.
- A *date* represents a particular day on a calendar. For example: May 21, 2022. The calendar you are primarily familiar with is the Gregorian calendar, although other calendar systems exist and have some limited use, particularly for holidays and other religious reasons.
- A *time* on its own represents a *time of day*—hours, minutes, seconds, and perhaps more depending on precision. For example: 7:45:00 PM.
Combining a *date* and a *time* gives you an precise instant in time, and is commonly called a *datetime* or *timestamp*. However, one more ingredient is needed to remove ambiguity: time zones.
Learning resources:
- TODO: uh is there really anything good to link here
2022-05-30 03:30:00 +00:00
## Time zones
2022-05-22 02:39:17 +00:00
You are already casually familiar with time zones. They reflect the very real fact that different locations on Earth experience the start and end of a day at different times. They are a major source of consternation for programmers, however, because they involve lots of human ambiguity and politics.
From a programmer's perspective, a time zone can be thought of as a bundle of the following:
- A *time offset* from UTC
- Rules for how this offset changes over time
The former is relatively easy to handle; the latter is fraught with politics and annoying edge cases, and will be discussed in the next section.
The time offset removes ambiguity by anchoring the time to UTC (Coordinated Universal Time), an international standard time that is continuous and unaffected by regional policy. A *timestamp* or *datetime* is thus a combination of date, time, and time zone or time offset. These three ingredients represent a fully-qualified, unambiguous instant in time.
It is worth noting that the time offset may not always be in increments of one hour. There are several time zones in current use that are offset by 30 or 45 minutes from the typical one-hour interval, and historical timezones that predate modern timekeeping may be offset by any arbitrary amount.
Resources:
- TODO: Definition of UTC
- https://www.timeanddate.com/time/time-zones-interesting.html (not a super great resource? find a better one?)
2022-05-30 03:30:00 +00:00
### Aside: UTC and the International Date Line
2022-05-22 02:39:17 +00:00
Broadly speaking, UTC (Coordinated Universal Time) is the date and time at the prime meridian (zero degrees longitude), and is not subject to daylight savings or other discontinuities.
You may have also heard of GMT (Greenwich Mean Time). GMT is also centered at the prime meridian, but is UK-centric and therefore subject to some historical nonsense. UTC is the internationally standard successor, and the one appropriate for computer use.
The existence of the prime meridian necessitates a date cutoff point at roughly 180 degrees longitude. This is the International Date Line, the point at which you cross from UTC-12 to UTC+12 or vice versa, changing the date as necessary.
You might think that the International Date Line ensures that no two locations on earth have a time difference greater than 24 hours. But naturally, there are in fact time zones whose time offset is greater than 12 hours from UTC. Rules are made to be broken.
Resources:
- TODO: Definition of GMT and how it relates to BST and other British politics
- TODO: Definition of the IDL
2022-05-30 03:30:00 +00:00
## Daylight saving time and locations
2022-05-22 02:39:17 +00:00
Many locations on Earth observe regular changes to their timekeeping, shifting their clocks forward or backward by one hour to put sunrise and sunset in a different place relative to their working day. Many people, especially programmers, think this is stupid, but we programmers are stuck with it until every government on Earth decides to abolish this practice.
The most important implication of daylight saving time is that **you must not assume that a day is 24 hours long.** A calendar day may be 23 or 25 hours long, depending on the direction of the transition. The visible impact of this is that the day will either repeat or skip an hour, and this change may occur at different times of day depending on the location.
For example, time in Chicago on March 14, 2021 proceeded directly from 1:59:59 CST (Central Standard Time) to 3:00:00 CDT (Central Daylight Time). Likewise, on November 7, 2021, time proceeded directly from 1:59:59 CDT to 1:00:00 CST. Note that when repeating an hour, the time zone resolves the ambiguity.
To help you deal with daylight saving time, you may have heard mnemonics such as "spring forward, fall back", or the terms "gain an hour" and "lose an hour". These terms are unfortunately confusing and appear at times to be in conflict, so here is a cheat sheet:
2022-05-22 02:41:43 +00:00
| Coming from | Going to | Occurs in | Clock adjustment | Lived experience | Analogous to |
| ----------- | -------- | --------- | ---------------- | ---------------- | -------------- |
| Standard | DST | Spring | "Spring forward" | "Lose an hour" | Traveling east |
| DST | Standard | Fall | "Fall back" | "Gain an hour" | Traveling west |
2022-05-22 02:39:17 +00:00
A useful gut-check for all this is to remember that the Sun rises in the east and sets in the west. Traveling west would thus mean traveling with the Sun and experiencing more daylight—hence, "gaining an hour", and having to adjust your clock _backward_ to account for the extra time in your day.
> Aside: there are many conflicting explanations for who originally proposed the practice of daylight saving time, why governments actually chose to adopt this practice, and why it has continued to this day. This article does not attempt to clarify any of this, but the author chooses to believe that all this is Benjamin Franklin's greatest prank of all time.
Resources:
- TODO
2022-05-30 03:30:00 +00:00
## Leap days and leap seconds
2022-06-02 04:00:03 +00:00
Unfortunately, one year on the Gregorian calendar does not correspond exactly to one solar year. You're probably already aware of how this is handled; every four years, February has an extra day. (Except for years divisible by 100, except for those which are also divisible by 400.) This is not very hard to handle, and it will be accurate enough for at least the next 1000 years.
2022-05-30 03:30:00 +00:00
Unfortunately, there is a more difficult problem - the Earth's rotation is somewhat irregular. A variety of geological effects can cause the Earth's rotation to speed up or slow down, causing a solar day to step out of sync with our clocks. In order to avoid this drift, _leap seconds_ are occasionally observed, extending or contracting the length of the day by one second. Unlike leap years, there is no set schedule for leap seconds - the Earth's rotation is not predictable enough. Computer systems must periodically download new timekeeping information so they always know of any upcoming leap seconds.
Since the year 1972, 27 leap seconds have been observed. At the time of this writing, a negative leap second (shortening the day) has never been observed; only positive leap seconds have been observed (lengthening the day). However, negative leap seconds are possible in theory, and they may someday be required.
All of this has some consequences for programmers:
- You should not assume that a year has 365 days (because a leap year may make it 366).
- You probably should not assume that a minute has 60 seconds (because a leap second may make it 59 or 61).
- 23:59:60 may be a valid time, depending on the day.
2022-06-02 04:00:03 +00:00
The irregular and unpredictable nature of leap seconds has resulted in varying implementations. One common technique for dealing with leap seconds is essentially to ignore them: it is common for time synchronization servers to "smear" the leap second over a period of time, causing their clients to slowly drift until they match with real UTC again. In this case, clients are completely unaware that a leap second is occurring. More information about leap smearing can be found in this article:
2022-05-30 03:30:00 +00:00
- https://docs.ntpsec.org/latest/leapsmear.html
2022-06-02 04:00:03 +00:00
[NOTE(ben): It kind of feels like we should have covered NTP in some limited way by now?]
2022-05-30 03:30:00 +00:00
Resources:
- IERS (TODO)
- A site exploring the possibility of eliminating leap seconds by redefining a calendar day in UTC: https://www.ucolick.org/~sla/leapsecs/
## Date representations, the Unix epoch, and the year 2038 problem
2022-06-02 04:00:03 +00:00
With all these concepts established, the question now is how to actually represent these in your application. For most uses of eeeboo beeboo
- Unix timestamp
- Seconds since January 1, 1970 UTC
- Unambiguously stores an instant in time, but _nothing else_
- Somewhat ambiguous for dates before 1972 (because UTC was not established yet)
- Trivial computer representation (a single number)
- Not human readable
- RFC 3339 date-time
- Unambiguously stores an instant in time
- Indicates offset from UTC (but _not_ time zone in the proper sense - no DST)
- Human-readable
- Restricted subset of ISO 8601
- You literally have no reason to use ISO 8601. Just don't. A copy of the spec costs hundreds of dollars, and it allows way more formats than anyone needs. Anyone who claims to accept "ISO 8601 timestamps" is wrong; they probably actually support RFC 3339 and a few variants. Meanwhile, RFC 3339 is freely available and has a small but complete set of features.
- Contains useful definitions for both date and time encoding
-
| | | |
| -------------- | ---- | ---- |
| Unix timestamp | | |
| | | |
| | | |
2022-05-22 02:39:17 +00:00
- RFC 3339 and its pitfalls
- Other profiles (ISO8601)
- IANA tzdb
2022-06-02 04:00:03 +00:00
# What time is it actually: time synchronization
- NTP/chrony
- forkingpaths help you are the one who is the smart
TODO: short intro
2022-05-30 03:30:00 +00:00
## The NTP Network
To maintain a consistent notion of time accross the internet, you first need some reference time signal, which is typically provided by atomic clocks. Since it is neiter practical nor desirable to connect every computer in the world to these atomic clocks, the time signal is distributed accross a network of peers organized in serveral strata. Primary servers sit at stratum 1 and are directly connected (through wire or radio) to a reference clock. Peers at strata 2 can synchronize on servers at strata 1 or 2, and act as a synchronization sources for peers at strata 3. The layering continues up to stratum 15. Strata 16 represents unsynchronized devices.
![NTP architecture flow graph](./NTP_hierarchy.svg)
The protocol selects sources in order to avoid loops and minimize the round-trip delay to a primary server. As such peers collaborate to automatically reorganize the network to produce the most accurate time, even in the presence of failures in the network.
2022-06-04 11:14:22 +00:00
## NTP Architecture
Now let's see the implementation of one node of the network. The following diagram shows the various stages of the pipeline used to collect time information and mitigate errors caused by network delays and clock inaccuracies. I'll explain each one in a short paragraph and give links to dig deeper.
2022-06-03 22:16:58 +00:00
![NTP architecture flow graph](./NTP_architecture_summary.svg)
2022-05-22 02:39:17 +00:00
2022-06-04 11:14:22 +00:00
### On-Wire Protocol
The first stage consists of getting time estimates from each peer. In the simplest mode of operations, the node polls a peer by sending a request packet timestamped with the transmission time, $t_1$. Upon reception of the request, the peer stores a timestamp $t_2$, processes the message, and sends a response packet containing $t_1$, $t_2$ and the response transmission time $t_3$. The node receives the response at time $t_4$.
![NTP architecture flow graph](./NTP_on_wire.svg)
The node then compute the tuple $(\delta, \theta, \epsilon)$, where:
- $\delta$ is the round-trip delay.
- $\theta$ is the estimated offset of the server clock with respect to the local clock.
- $\varepsilon$ is the dispersion of the sample, i.e. the maximum error due to the frequency tolerances $\varphi_p$ and $\varphi_c$ of the peer's clock and of the client's clock, and the time elapsed since the request was sent.
$\begin{align*}
\delta = (t_4 - t_1) - (t_3 - t_2) \, ,\qquad
\theta = ((t_2 - t_1) + (t_3 - t_4))/2 \, ,\qquad
\varepsilon = \varphi_p \times \varphi_c \times \delta\,.
\end{align*}$
Simple NTP clients that are only synchronized to one server, that don't act as a source for other peers, and that don't need a high precision, can implement only the on-wire protocol and directly use $\theta$ to correct their local clock. However this estimate is vulnerable to network jitter, delay asymetry, clock drift and wander. To get better precision, the samples produced by the on-wire protocol must be processed by a number of mitigation algorithms.
Links: [Analysis and Simulation of the NTP On-Wire Protocols](https://www.eecis.udel.edu/~mills/onwire.html), [RFC5905 - section 8](https://datatracker.ietf.org/doc/html/rfc5905#section-8)
### Clock Filter
### Clock Selection
### Cluster Algorithm
### Clock Discipline
### Clock Adjust
2022-05-30 03:30:00 +00:00
# How long things take: durations
2022-05-22 02:39:17 +00:00
Computer clocks
---
TODO:
- Style check:
- em dashes
- "Earth" and "Sun" are always capitalized
- "time zone" is two words
- "daylight saving time", not "daylight savings time"
---
EVERYTHING BELOW HERE IS OUR INITIAL BRAINSTORM
This will be our article three. Ben will get to this when he has time, unless someone else wants to jump in and lead the content of this.
- Timezones
- They are not always on the hour
- Difference time offset and timezone
- (locale stuff, formatting, DST transition dates, etc.)
- Daylight savings
- Days can be 23 or 25 hours long
- Countries change their DST rules all the time
- Dates, times, datetimes
- Maybe you don't actually want to store full timestamps for literally everything
- Time math is tricky
- Adding 24 hours != adding one day (leap days / seconds)
- Calendars
- Historical calendars are very weird (kings just removing or adding days when they felt like it)
- The calendar we use, and its extensions: Gregorian calendar, proleptic Gregorian calendar
- Other calendars: Jewish calendar, Chinese lunar calendar (?), Hijiri, Japanese calendar (?)
- Primarily used for establishing holidays, but not really for common use any more (with the exception of China...?)
- Computer clocks
- CPU time vs. wall time
- Monotonic clocks
- Two kinds on Linux: one that smears forward, one that does not
- Jiffies
- Atomic clocks
- NTP / chrony
- Useful specs:
- RFC3339: https://datatracker.ietf.org/doc/html/rfc3339
- iCal: https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/
- Recurrence rules (RRULE): https://icalendar.org/iCalendar-RFC-5545/3-8-5-3-recurrence-rule.html
- NOBODY USES ISO8601 STOP SAYING THAT THEY DO
- NO ONE KNOWS WHAT IT IS
- RFC3339 IS THE ONLY ONE ANYONE SHOULD USE
- Accuracy of onboard clocks
- Quartz oscillators - subject to drift
- How to correct inaccurate clocks
- Time smearing
- Presentation and formatting?
- The UNIX epoch, and the year 2038 problem
- ISO "week date" (does this matter to anyone)
- Falsehoods I think are actually important
- "Always store time in UTC"
- "Always store fully-qualified timestamps" i.e. datetimes with timezone, not just dates
- "The smallest unit of time is X" (i.e. store the start and end of your time ranges precisely, never store 11:59:59)
2022-05-30 03:30:00 +00:00
- Other advice
- If anyone ever tells you to "store things for a month", clarify whether you need an actual calendar month or simply a 30-day period.
2022-05-22 02:39:17 +00:00
You get a time you trust - then you work with it in some way (???)
- https://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time
- https://engineering.fb.com/2021/08/11/open-source/time-appliance/
- https://github.com/opencomputeproject/Time-Appliance-Project/tree/master/Open-Time-Server/
- https://lettier.github.io/posts/2016-04-26-lets-make-a-ntp-client-in-c.html
- https://docs.ntpsec.org/latest/leapsmear.html
- https://aws.amazon.com/blogs/aws/look-before-you-leap-the-coming-leap-second-and-aws/
- https://sudonull.com/post/29903-Writing-a-Simple-NTP-Client
- https://www.oreilly.com/library/view/linux-device-drivers/9781785280009/4041820a-bbe4-4502-8ef9-d1913e133332.xhtml
- https://www.youtube.com/watch?v=-5wpm-gesOY
- Official timezone database: https://www.iana.org/time-zones
- Lots of excellent information and links here: https://data.iana.org/time-zones/tz-link.html
- https://data.iana.org/time-zones/tz-how-to.html
- "When Alaska was purchased from Russia in 1867, the Date Line moved from the Alaska/Canada border to the Bering Strait; and the time in Alaska was then 24 hours earlier than it had been. <aside>(6 October in the Julian calendar, which Russia was still using then for religious reasons, was followed by a second instance of the same day with a different name, 18 October in the Gregorian calendar. Isnt civil time wonderful? 8-))</aside>"
## How to use dates and times
## Time accuracy / how to get accurate times
## Fun tangents
East Asian countries treat birthdays differently: https://en.wikipedia.org/wiki/East_Asian_age_reckoning. In particular, Koreans (and others??) all celebrate their birthday on January 1, and start counting at 1 instead of 0.