Hacker News new | ask | show | jobs
by smokel 1044 days ago
Date and time handling in software is one of the most dramatic examples of software being a means of communication between humans and machines. What appears to be a seemingly basic physical process turns out to demand reams of code to manage leap years, leap seconds, time zones, the birth of Christ and that of Unix, Gregorian and Julian calendars, and worst of all: daylight saving time (DST).

DST was once supposed to save energy, but my own personal frustration with it alone could warm the Earth's atmosphere by a whole degree Celsius.

4 comments

Most of confusion can be resolved by introducing 3 separate entities (and naming them properly):

- Instant - a point in monotonously increasing time (Unix timestamp is usually a good enough approximation for most cases)

- Calendar

- Wallclock - non-monotonous human-readable clock

Being precise about which entity is used is enough to resolve most of the confusions.

For example, scheduling a doctor appointment is always Calender + Wallclock in a location of doctor's office. No Instant should be used to record this, and changing of DST rules for this location shouldn't affect the appointment. DST rules only matter if we want to convert from one Calendar and Wallclock to the other, then Instant may be used internally in calculations.

Back to the article: if we are talking about subscriptions, then we should specify, if subscription is valid for time interval of 365*24*60*60 seconds or the expiry is tied to Calendar+Wallclock, but in this case Calendar+Wallclock should have a specific location. The first option is much easier to implement, and to avoid any timezone/DST frustration adding an extra day of subscription (366 instead of 365) will be enough.

I think in most systems, calendar is called LocalDate and Wallclock is either LocalTime or combined with a LocalDate into a LocalDateTime.
I remember having a bit of fun computing the number of work hours between two dates.

It involved:

- the day of the week (office closed on weekends)

- day of the year, with leap years (office closed during holidays)

- Easter day (some holidays are based on Easter)

- DST and timezones (office hours are in local time, timestamps are UTC)

Thankfully, no Julian calendar or leap seconds to deal with.

Many problems would be solved by using TAI for storage instead of timezones/UTC. At least for past events.
Save the planet, remove daylight saving