Time
Time is a quite a tricky subject for software. Most people have an established intuitive perception about time, so it’s easy to oversimplify and overlook edge cases. In addition, although the concept of time is simple, there are at least three distinct versions of it, and they aren’t always synchronised.
Cosmic time is passing in the real world without any care for humans. It’s governed by the laws of physics. Although Einstein famously declared it to be relative, for most purposes it’s the same everywhere on planet Earth. It’s also continuous and, except in bad science fiction, always moves in a single direction.
Elapsed time deals with periods between two reference points in cosmic time. This is the time we can measure, and deals with periods such as seconds. Elapsed time doesn’t have any notion of midnight, summer or Tuesday next week. This type of time is a human invention, but apart from someone choosing the two reference points for measurement, it doesn’t depend on humans. Instead, it depends on the recording machinery. For computers today, this mostly means that elapsed time isn’t continuous, but discrete, in increments of milliseconds. Theoretically, it should be the same everywhere on Earth, but practically it’s not. Measuring devices use different precision and accuracy. Your computer and your mobile phone may measure the same period differently, and they both diverge slightly from NIST-F1, the atomic clock that controls the official time in the USA. Lastly, elapsed time isn’t infinite. It’s subject to the capacity of measurement equipment, which is why many older computers can’t see beyond 2038.
Clock time is the one used for calendars, to guide our daily lives, schedule meetings and keep society synchronised. Clock time deals with concepts such as 14:45pm, wake-up alarms, and beer o’clock. It’s different in different places of the world, driven by solar cycles, the Earth’s rotation, and the needs of the communities living in a particular area. It’s a uniquely human thing, subject to politics, government conventions and manipulation. It can jump ahead, move backwards, stall or stretch.
Most of the time, excuse the pun, the three types are the same for all practical purposes. But the problems start when they suddenly diverge, even if only for a moment. Here are some commonly overlooked quirks of time that cause problems:
Days aren’t always exactly 86,400 seconds long. Leap seconds can make a day one second longer. (31 December 2016 had a leap second.)
Coordinated Universal Time (UTC) and Greenwich Mean Time (GMT) time zones aren’t always the same. They can drift by up to 0.9 seconds. That’s why leap seconds exist.
Leap seconds don’t necessarily have to be positive. If the Earth’s rotation required it, it would theoretically be possible to introduce a negative leap second. This has never happened so far – but once it does, better stay home that day.
Years aren’t always 365 days long. Remember the occasional 29 February.
Leap years don’t happen every four years. Three out of four end-of-century years don’t have a leap day.
Clock time doesn’t always go forwards. Daylight saving can make it jump backwards.
Computers couldn’t care less about clock time, they only deal with elapsed time. Applying clock time arithmetic to elapsed time often leads to problems. For example, adding one month to the current time doesn’t produce a period of exactly one month in clock time. Time zones, different numbers of days in a month and other exceptions can cause wrong calculations.
Scheduling future events by using elapsed time is dangerous. For example, ‘same time tomorrow’ isn’t always 24 hours away. Daylight saving can shift the clock. People might travel into a different time zone. The longer the period, the more chance of a mess.
Clock time and elapsed time don’t always move by the same amount. Daylight saving can create big gaps.
Daylight saving time isn’t applied consistently across all countries, or even within a single country. For example, most of the USA observes daylight saving time, but Hawaii does not. In Australia, New South Wales observes daylight saving time, but Queensland does not.
The daylight saving time schedule isn’t fixed. It’s a political agreement, subject to change. For example, Israel synchronised time zone changes with the east of Europe in 2013, moving the end of summer time from early September to late October.
Elapsed time isn’t always positive. Most computers represent dates before 1970 as negative numbers.
Unlike cosmic and clock time, elapsed time isn’t infinite. Check the numeric limits of your date records, and test around those. For example, for typical 32-bit dates, test for dates before 1970 and after February 2038.
Missing or invalid time might mask itself as 1 January 1970 or 31 December 1969, especially if you’re using third-party components outside your control.