Skip to content

ToField UTCTime sub-microsecond rounding #154

@avieth

Description

@avieth

Postgres will parse the time part after the decimal point to a double, then multiply by 1 million, then round to an integer. This can lead to some strange behaviour:

ghci> query conn "select (?)::timestamptz" (Only (UTCTime (fromGregorian 1970 1 1) 0.0000025)) :: IO [Only UTCTime]
[Only {fromOnly = 1970-01-01 00:00:00.000002 UTC}]

That one appears to round down, but this one appears to round up

ghci> query conn "select (?)::timestamptz" (Only (UTCTime (fromGregorian 1970 1 1) 0.5187445)) :: IO [Only UTCTime]
[Only {fromOnly = 1970-01-01 00:00:00.518745 UTC}]

because

ghci> ((0.5187445 :: Double) * (1000000 :: Double))
518744.50000000006

Arguably ToField UTCTime shouldn't exist, since it can't really fit into a timestamptz. But that's probably not an acceptable breaking change so what else could be done about this? Maybe instance ToField UTCTime should truncate to microseconds?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions