Skip to content

Commit 3f18e1d

Browse files
authored
Merge pull request #4 from kachick/fix-generator-handles-milliseconds
Fix to handle timestamp precision in generator
2 parents 4a8341b + d6ed0fb commit 3f18e1d

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/ulid/generate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def random_bytes
6161
end
6262

6363
def millisecond_time
64-
(@time.to_f * 1_000).to_i
64+
(@time.to_r * 1_000).to_i
6565
end
6666

6767
# THIS IS CORRECT (to the ULID spec)

spec/ulid_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
expect(ULID.at(KNOWN_TIME)).to be_a_valid_ulid
2525
end
2626

27+
it 'handles timestamp as the milliseconds precision' do
28+
expect(ULID.at(Time.parse('2016-07-30 22:36:16.001000000 UTC'))).to start_with('01ARYZ6RR1')
29+
expect(ULID.at(Time.parse('2016-07-30 22:36:16.002000000 UTC'))).to start_with('01ARYZ6RR2')
30+
expect(ULID.at(Time.parse('2016-07-30 22:36:16.003000000 UTC'))).to start_with('01ARYZ6RR3')
31+
end
32+
2733
end
2834

2935
describe '.time' do

0 commit comments

Comments
 (0)