Skip to content

Instantly share code, notes, and snippets.

@Wizek
Created December 8, 2016 02:57
Show Gist options
  • Save Wizek/00f7d9c0908565bf609f6578aefeb2fe to your computer and use it in GitHub Desktop.
Save Wizek/00f7d9c0908565bf609f6578aefeb2fe to your computer and use it in GitHub Desktop.

How to render time duration as human readable with a Haskell package?

Anyone knows of a Haskell package that can render NominalDiffTime (or a similar type) as human readable time duration?

Some examples of what I mean:

f :: NominalDiffTime -> String
f 1 = "1 second"
f 10 = "10 seconds"
f 100 = "about 2 minutes"
f 10000 = "about 3 hours"
f 100000 = "about 1 day"
-- etc...

I've found a few that come close, but no cigar yet:

Which is capable of printing the most significant unit, e.g. "3 days ago", but as far as I can see it is always relative to now and has the postfix " ago"/" from now".

A straightforward workaround could be to ask for the " ago" variety and always drop the last 4 characters.

  • Furthermore, there is also: show (10000.123 :: NominalDiffTime) == "10000.123s"

    But it's not very useful to just get an "s" postfix in my usecase.

Anyone knows of any package that comes closer to what I am looking for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment