DEV Community

Serguey Asael Shinder
Serguey Asael Shinder

Posted on

Time Is the Hardest Type

Most types are easy.

A number is a number.

A name is a name.

Time is not like that.

Time looks like a number
and behaves like a negotiation.

A day is not always
twenty-four hours.

A minute is not always sixty seconds,
because somewhere a committee
added a leap second
and nobody told your code.

Midnight happens twice a year
in some places
and not at all in others.

Yesterday depends on
where the reader is standing.

And none of this shows up
in the type signature.

You write a date field,
and you have quietly made
a dozen decisions
you never wrote down.

Is this the moment something happened,
or the day somebody means?

An instant and a calendar day
are different things.

One is a point on a line.

The other is a label
that depends on a person.

Storing them in the same column
is where the reports start disagreeing.

Whose clock is authoritative?

The server's, the database's,
the browser's, the phone
in airplane mode since Tuesday?

Every one of them is confident.

They disagree by seconds,
and seconds are enough
to reorder events
that must not be reordered.

So be boring about it,
on purpose.

Store instants in one zone,
and say which one out loud,
in the name of the field
if you have to.

Convert at the edges,
where a human is looking.

Never in the middle,
where nobody is.

Take the clock as something
you are handed,
not something you reach for,
because a clock you are handed
can be handed to a test too.

And when a bug arrives
that only happens for one user,
once a year, at a strange hour,
do not call it impossible.

Ask what time it was
where they were standing.

Time is the one type
that is always also
a place,
and a rule somebody changed
without a release note.

– Serguey Asael Shinder

Top comments (0)