Skip to content

Commit 258df47

Browse files
committed
Fix parsing of timestamp part in ULIDs for small timestamp values
1 parent e7b55cd commit 258df47

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

tasklite-core/source/Utils.hs

+4-6
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@ parseUtc utcText =
5353
parseUlidUtcSection :: Text -> Maybe DateTime
5454
parseUlidUtcSection encodedUtc = do
5555
let
56-
decodedUtc = (Crock.decode . unpack) encodedUtc
56+
decodedUtcMaybe = (Crock.decode . unpack) encodedUtc
5757
getElapsed val = Elapsed $ Seconds $ val `div` 1000
58-
getMilliSeconds val =
59-
readMaybe $ T.unpack $ T.takeEnd 3 $ show val :: Maybe Int64
6058

61-
elapsed <- fmap getElapsed decodedUtc
62-
milliSeconds <- getMilliSeconds decodedUtc
59+
elapsed <- fmap getElapsed decodedUtcMaybe
60+
milliSecPart <- fmap (`mod` 1000) decodedUtcMaybe
6361

64-
let nanoSeconds = NanoSeconds $ milliSeconds * 1000000
62+
let nanoSeconds = NanoSeconds $ milliSecPart * 1000000
6563

6664
pure $ timeGetDateTimeOfDay $ ElapsedP elapsed nanoSeconds
6765

tasklite-core/test/Spec.hs

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ import Migrations
2424
-- | and therefore the order must not be changed
2525
testSuite :: Config -> DateTime -> Sql.Connection -> SpecWith ()
2626
testSuite conf now connection = do
27+
describe "Utils" $ do
28+
it "correctly parses beginning of UNIX epoch" $ do
29+
(parseUlidUtcSection "0000000000")
30+
`shouldBe`
31+
(Just $ timeGetDateTimeOfDay $ Elapsed 0)
32+
33+
it "correctly parses 36 ms after UNIX epoch" $ do
34+
(parseUlidUtcSection "0000000014")
35+
`shouldBe`
36+
(Just $ timeGetDateTimeOfDay $ ElapsedP 0 36000000)
37+
38+
it "correctly parses a ULID string" $ do
39+
let ulidText = "0000000014T4R3JR7HMQNREEW8" :: Text
40+
41+
(fmap show $ parseUlidText ulidText) `shouldBe` (Just ulidText)
42+
2743
describe "TaskLite" $ do
2844
let
2945
-- TODO: Make function more generic

0 commit comments

Comments
 (0)