Skip to content

Commit 4a8341b

Browse files
authored
Merge pull request #5 from kachick/fix-parser-handles-milliseconds
Fix to handle timestamp precision in parser (huge thanks to @kachick!)
2 parents fabb5b5 + 9824943 commit 4a8341b

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

lib/ulid/parse.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def unpack_decoded_bytes(packed_bytes)
5151
(time_bytes[1].to_i << 32) |
5252
(time_bytes[0].to_i << 40)
5353

54-
[ Time.at( time_int * 0.001 ).utc, seed ]
54+
[ Time.at( time_int * 1/1000r ).utc, seed ]
5555
end
5656

5757
end

spec/ulid_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
end
3535

3636
it 'returns same time that was used to generate it' do
37-
# NOTE: we may not get the PRECISE time out due to conversion from
38-
# Time -> Float -> Base32 -> Float -> Time
39-
#
40-
# It's not immediately clear to me whether that's a function of Ruby
41-
# or of the format.
42-
expect(ulid_time).to be_within(0.001).of(KNOWN_TIME)
37+
expect(ulid_time).to eq(KNOWN_TIME)
38+
end
39+
40+
it 'handles timestamp as the milliseconds precision' do
41+
time = ULID.time('0A000000000000000000000000')
42+
expect(time.to_i).to eq(10995116277)
43+
expect(time.nsec).to eq(760000000)
4344
end
4445
end
4546

0 commit comments

Comments
 (0)