Skip to main content

A standard to represent relative times

·2 mins
Cover image

Say you have an application that collects data from a number of sources and stores it somewhere. Some of this data is only worth keeping for a certain amount of time, before it becomes worthless.

So alongside the data, you might have an expires_on attribute, with a time in ISO 8601 format, and you implement a job that runs nightly and removes any expired data.

So far so good, but every time you collect the data, you need to work out when it should expire. Often it would be a time relative to the collection time, maybe 2 months, or 1 year. Instead of having the data collection add this time, why not have it provide a relative time instead and have the removal job work it out by using that against the collection time.

Let’s call this attribute expires_in. What does it look like? We probably want something like 2 months, or 1 year. And there are plenty of libraries that can parse this, such as Moment.js and Natty, though they are a bit over the top for our use.

But, unlike with dates and times, there isn’t a standard for this, so the parsing depends entirely on code you write or the library you use. What may be parsed and valid now might not be parsed later by different or new code.

This looks like a good place for a standard to be developed.

Update: We decided to use the ISO 8601 Durations standard. It’s a bit obscure compared to what I suggested above, but at least it is a standard that can be consistently parsed.

Cover image by [Sean MacEntee](