haskell / time

A time library
http://hackage.haskell.org/package/time
Other
120 stars 80 forks source link

Read instance for NominalDiffTime #130

Closed MaxGabriel closed 4 years ago

MaxGabriel commented 4 years ago

Hi, I'd like to propose adding a Read instance for NominalDiffTime.

My specific use case

I was recently working on adding configurable idle timeouts to the Persistent database library, which uses resource-pool under the hood, which uses a NominalDiffTime for describing how long to let an idle resource sit around before disposing of it.

Unfortunately, the configuration type in the Persistent codebase derived Read, so adding a NominalDiffTime field to it caused issues because NominalDiffTime does not have a Read instance. I can workaround this by e.g. having an Integer field that I convert to a NominalDiffTime, it's just slightly less nice.

To be honest, I try to avoid Read as much as I can, and I don't really think the Persistent type needs a Read instance. That said, Read is a pretty foundational typeclass in Haskell, and time is a pretty foundational package, so it would be nice if its types had Read instances.

Implementation

The type already has a Show instance implemented like this:

instance Show NominalDiffTime where
    show (MkNominalDiffTime t) = (showFixed True t) ++ "s"

It doesn't look like there is an analogous function to showFixed for reading, which might make implementing a Read instance annoying.

AshleyYakeley commented 4 years ago

Done.