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 anSqlLocalTime
which can not be converted toUTCTime
byfromSql
. - If I try to use
DATE_FORMAT()
in SQL to get ISO8601 compliant strings (adding trailing+00:00
orZ
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 ?
SqlUTCTime
is YYYY-MM-DD HH:MM:SS. Perhaps you could try usingDATE_FORMAT
to produce that, rather than an 8601-compliant string, and see what happens.