Add section on leap stuff

This commit is contained in:
Ben Visness 2022-05-29 22:30:00 -05:00
parent 2f70356c68
commit d511ad3d54
1 changed files with 37 additions and 10 deletions

View File

@ -6,7 +6,8 @@ There are two major (and largely separate) concerns related to time: *when thing
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.
## When things happen: instants in time
# When things happen: instants in time
The first major aspect of time for a programmer is the study of *when things happen*. This is the realm of calendars and clocks.
@ -21,7 +22,7 @@ Learning resources:
- TODO: uh is there really anything good to link here
### Time zones
## Time zones
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.
@ -41,7 +42,7 @@ Resources:
- TODO: Definition of UTC
- https://www.timeanddate.com/time/time-zones-interesting.html (not a super great resource? find a better one?)
#### Aside: UTC and the International Date Line
### Aside: UTC and the International Date Line
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.
@ -56,7 +57,7 @@ Resources:
- TODO: Definition of GMT and how it relates to BST and other British politics
- TODO: Definition of the IDL
### Daylight saving time and locations
## Daylight saving time and locations
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.
@ -79,15 +80,40 @@ Resources:
- TODO
### Leap days and leap seconds
- Within your lifetime, leap seconds will begin occurring about once a year - a very practical concern!
- Negative leap seconds possible
### Date representations, the Unix epoch, and the year 2038 problem
## Leap days and leap seconds
Unfortunately, one year on the Gregorian calendar does not correspond exactly to one solar year. You're already well 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 has no significant accuracy issues for the next millenium.
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.
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 NTP 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 these cases, clients are completely unaware that a leap second is occurring. More information about leap smearing can be found in this article:
- https://docs.ntpsec.org/latest/leapsmear.html
More on NTP later.
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
- RFC 3339 and its pitfalls
- Other profiles (ISO8601)
- IANA tzdb
## How do I actually know what time it is
- NTP
## How long things take: durations
# How long things take: durations
Computer clocks
@ -148,6 +174,8 @@ This will be our article three. Ben will get to this when he has time, unless so
- "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)
- 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.
You get a time you trust - then you work with it in some way (???)
@ -155,7 +183,6 @@ You get a time you trust - then you work with it in some way (???)
- 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://audiogramii.wordpress.com/2018/03/31/time-smearing/
- 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