How to get an SqlUTCTime in Haskell HDBC?

30 views Asked by At

I have a MariaDB database which I query through Haskell HDBC. In it I store a datetime column in UTC time. I would like to retrieve the values as SqlUTCTime and perform the conversion to UTCTime using fromSql.

  • If I retrieve the values directly in SELECT, I get an SqlLocalTime which can not be converted to UTCTime by fromSql.
  • If I try to use DATE_FORMAT() in SQL to get ISO8601 compliant strings (adding trailing +00:00 or Z to indicate UTC), I get errors that the strings are not compliant.

I ultimately managed to convert manually from SqlLocalTime to SqlUTCTime :

convertSqlValue :: SqlValue -> Maybe UTCTime
convertSqlValue sql = case sql of
    SqlString s -> iso8601ParseM s
    SqlUTCTime u -> Just u
    SqlLocalTime l -> Just $ localTimeToUTC utc l
    other -> Nothing

where the SqlLocalTime is called and localTimeToUTC utc l performs the conversion.

My question therefore is: what should I do to get an SqlUTCTime directly ?

0

There are 0 answers