fix: Using built-in cache of getters for ULID#time
parent
fb93e63fe5
commit
0682973758
12
src/ulid.cr
12
src/ulid.cr
|
|
@ -13,6 +13,11 @@ module Ulid
|
||||||
def_hash @bytes
|
def_hash @bytes
|
||||||
|
|
||||||
getter bytes : Bytes
|
getter bytes : Bytes
|
||||||
|
getter time : Time do
|
||||||
|
ms = 0u64
|
||||||
|
6.times { |i| ms |= @bytes[5 - i].to_u64 << (i * 8) }
|
||||||
|
Time.unix_ms(ms)
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(bytes : Bytes)
|
def initialize(bytes : Bytes)
|
||||||
initialize(bytes, true)
|
initialize(bytes, true)
|
||||||
|
|
@ -56,13 +61,6 @@ module Ulid
|
||||||
io << ">"
|
io << ">"
|
||||||
end
|
end
|
||||||
|
|
||||||
def time : Time
|
|
||||||
return time if time = @time
|
|
||||||
ms = 0u64
|
|
||||||
6.times { |i| ms |= @bytes[5 - i].to_u64 << (i * 8) }
|
|
||||||
@time = Time.unix_ms(ms)
|
|
||||||
end
|
|
||||||
|
|
||||||
def <=>(other : ULID)
|
def <=>(other : ULID)
|
||||||
@bytes.<=>(other.@bytes)
|
@bytes.<=>(other.@bytes)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue