Skip to content

Commit

Permalink
Add support for parsing unix timestamps with 10, 13, and 16 digits
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Mar 8, 2020
1 parent ac15d47 commit ce0392d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tasklite-core/source/ImportExport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions tasklite-core/source/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ce0392d

Please sign in to comment.