Skip to content

How I think about datetime in systems engineering

Published: at 04:32 PM

After spending something like ten years building systems which tracked and planned physical operations I had an insight that finally allowed me to understand date/time problems:

Time is relative to somewhere on Earth. To know when something happens, you have to know where. There is no such thing as time in the abstract

Metaphysical philosophy might take issue with this, but I am not a metaphysicist, just a software engineer. Following this rule, in my experience, helps to solve the many follow-on problems with datetimes, especially regarding time zones, duration calculations, and daylight savings time. More importantly, it helps me to simply think through the thorny datetime issues that arise while building applications that leverage datetimes (which is practically every application).

There are some other helpful rules of thumb that I follow:

Storing and Transmitting Datetime:

These rules are for the human programmers trying to debug and maintain systems, not necessarily for the systems themselves. If all datetimes are in ISO 8601 with a consistent TZ offset, then it is much easier to understand traces and logs and answer questions bugs.

Displaying Datetime:

Timezone calculations:

While thinking about timezones, use the general timezone identifier, e.g., “Eastern Time” instead of “Eastern Daylight Time” or “Eastern Standard Time”. Or even better, use the location-based identifier e.g., “America/New_York” (again, time is relative to location). Good datetime libs will correctly handle daylight savings time conversions depending on the time of year. Updates to daylight savings time do happen, and it’s important to keep libraries up-to-date to support this.

ICANN maintains the “tz database” which is should be the official source for timezone information for applications and contains the official location names. Any good datetime library should be up-to-date with this database.

Summary

To summarize, time is relative to location. Storing datetime in ISO 8601 with a consistent tz offset helps programmers understand the system and makes writing business logic, writing test cases, and reading logs much easier. Always display datetimes with a timezone. Leverage datetime libraries for conversions. If you store datetimes consistently and use general tz identifiers or location tz identifiers while using off-the-shelf library duration calculation functions, you don’t need to think about daylight savings or even leap days.


Next Post
Street photography