From 8cad65ff0cbb1ef56892bc132354c87866031dcd Mon Sep 17 00:00:00 2001 From: matthew Date: Tue, 15 Aug 2017 18:37:51 +0000 Subject: [PATCH] adding FromField and ToField instances for UTCTime --- Data/Csv/Conversion.hs | 12 ++++++++++++ cassava.cabal | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Data/Csv/Conversion.hs b/Data/Csv/Conversion.hs index 5746120..2867d47 100644 --- a/Data/Csv/Conversion.hs +++ b/Data/Csv/Conversion.hs @@ -119,6 +119,9 @@ import Data.Traversable (traverse) import Data.Word (Word) #endif +import Data.Time.Clock (UTCTime) +import Data.Time.Format (parseTimeM, defaultTimeLocale, formatTime) + ------------------------------------------------------------------------ -- bytestring compatibility @@ -1053,6 +1056,15 @@ instance ToField [Char] where toField = toField . T.pack {-# INLINE toField #-} +instance FromField UTCTime where + parseField t = + parseTimeM True defaultTimeLocale "%F" (T.unpack . T.decodeUtf8 $ t) + +instance ToField UTCTime where + toField t = + T.encodeUtf8 $ T.pack $ formatTime defaultTimeLocale "%F" t + + parseSigned :: (Integral a, Num a) => String -> B.ByteString -> Parser a parseSigned typ s = case parseOnly (ws *> A8.signed A8.decimal <* ws) s of Left err -> typeError typ s (Just err) diff --git a/cassava.cabal b/cassava.cabal index 467cb7f..ddbb395 100644 --- a/cassava.cabal +++ b/cassava.cabal @@ -103,7 +103,8 @@ Library text < 1.3, unordered-containers < 0.3, vector >= 0.8 && < 0.13, - Only >= 0.1 && < 0.1.1 + Only >= 0.1 && < 0.1.1, + time if flag(bytestring--LT-0_10_4) build-depends: bytestring < 0.10.4