From ce0392d720c417d54be26dfed167f65c90813e5e Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Sun, 8 Mar 2020 18:11:52 +0000 Subject: [PATCH] Add support for parsing unix timestamps with 10, 13, and 16 digits --- tasklite-core/source/ImportExport.hs | 5 +++-- tasklite-core/source/Utils.hs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tasklite-core/source/ImportExport.hs b/tasklite-core/source/ImportExport.hs index 4e6835f..4030cb3 100644 --- a/tasklite-core/source/ImportExport.hs +++ b/tasklite-core/source/ImportExport.hs @@ -76,14 +76,15 @@ data ImportTask = ImportTask -- | Values a suffixed with a prime (') to avoid name collisions instance FromJSON ImportTask where parseJSON = withObject "task" $ \o -> do - utc <- o .:? "utc" + utc <- o .:? "utc" <|> (o .:? "utc" :: Parser (Maybe Int)) entry <- o .:? "entry" creation <- o .:? "creation" created_at <- o .:? "created_at" + let zeroTime = timeFromElapsedP 0 :: DateTime parsedCreatedUtc = parseUtc - =<< (utc <|> entry <|> creation <|> created_at) + =<< ((fmap show utc) <|> entry <|> creation <|> created_at) createdUtc = fromMaybe zeroTime parsedCreatedUtc o_body <- o .:? "body" diff --git a/tasklite-core/source/Utils.hs b/tasklite-core/source/Utils.hs index a736c6d..33fb7fc 100644 --- a/tasklite-core/source/Utils.hs +++ b/tasklite-core/source/Utils.hs @@ -32,11 +32,22 @@ x <++> y = flip (<$>) +parseUtcNum :: Int -> Maybe DateTime +parseUtcNum number = + parseUtc (show number) + + parseUtc :: Text -> Maybe DateTime parseUtc utcText = let utcString = unpack $ T.toLower utcText + -- TOOD: Remove after https://github.com/vincenthz/hs-hourglass/issues/50 + addSpaceAfter10 = T.intercalate " " . T.chunksOf 10 + addSpaceAfter13 = T.intercalate " " . T.chunksOf 13 + unixMicro = "EPOCH ms us" :: [Char] + unixMilli = "EPOCH ms" :: [Char] + tParse :: [Char] -> Maybe DateTime tParse formatString = timeParse (toFormat formatString) utcString @@ -48,6 +59,11 @@ parseUtc utcText = <|> (tParse "YYYY-MM-DD H:MI:S") <|> (tParse "YYYY-MM-DD H:MI") <|> (tParse "YYYY-MM-DD") + <|> timeParse + (toFormat unixMicro) + (unpack $ (addSpaceAfter10 . addSpaceAfter13) utcText) + <|> timeParse (toFormat unixMilli) (unpack $ addSpaceAfter10 utcText) + <|> (tParse "EPOCH") parseUlidUtcSection :: Text -> Maybe DateTime