postgresql-simple
postgresql-simple copied to clipboard
Missing instance FromField NominalDiffTime
There is a ToField
instance, but FromField
is missing.
There's actually a reason for that: you can't really blindly convert months to days, or days to hours/minutes/seconds, without further context regarding the specific time and location you are talking about.
Basically, NominalDiffTime
is a proper subset of postgresql's interval
type (modulo precision and range issues), and interval
is a strict superset of NominalDiffTime
.
That said, I have considered adding a FromField NominalDiffTime
instance that assumes any intervals can be converted to seconds directly; any intervals that contain non-convertible components would result in an exception.
Hmm, maybe interval
is not the right field type for NominalDiffTime
time then?
I've also noticed that you don't have instances for Data.Fixed
, is there a reason for that too?
If not, I could use ToField
/FromField
instances of Pico
to store NominalDiffTime
.
My apologies for forgetting to respond to this issue.
I guess, what's your goal here? If all you care about is storing NominalDiffTime
s, and getting them back out with full fidelity, and you don't care about the semantics or having postgresql perform computations on them server-side, then you would have a few options. However, these options probably would not be a good fit for inclusion in vanilla postgresql-simple.
If you care about semantic similarity, which is the primary goal of vanilla postgresql-simple, then what option is there other than interval
? A postgresql interval corresponds to data Interval = Interval { months :: Int32, days :: Int32, microseconds :: Int64 }
, whereas a NominalDiffTime
is an Integer
of picoseconds. So, there's a lot of overlap, and NominalDiffTime
is almost a subset of interval
, but each type can represent some values the other can't.
I've also noticed that you don't have instances for
Data.Fixed
, is there a reason for that too?
Not that I'm aware of.
@lpsmith Is there any reason not to include your Interval
ADT in this library with the appropriate ToField/FromField conversions? It sounds like that's a pretty safe addition from a correctness perspective and it would allow first-class two-way conversion of Intervals <-> Haskells.
I think it would be a worthy addition to postgresql-simple, and I would welcome a pull request of reasonable quality.
In addition, I would probably accept a pull request that adds a FromField
instance for NominalDiffTime
, as well as Fixed
.
Though I may have looked into supporting Fixed
once, and was a bit scared off by some unfortunate ways postgres's numeric
type works. My memory is a bit fuzzy on that count.