From 0682973758e3082d5758ec1536c96b55e614e623 Mon Sep 17 00:00:00 2001 From: LeMarsu Date: Mon, 30 Sep 2019 19:03:52 +0200 Subject: [PATCH] fix: Using built-in cache of getters for ULID#time --- src/ulid.cr | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/ulid.cr b/src/ulid.cr index e1ff529..3b8f1a9 100644 --- a/src/ulid.cr +++ b/src/ulid.cr @@ -13,6 +13,11 @@ module Ulid def_hash @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) initialize(bytes, true) @@ -56,13 +61,6 @@ module Ulid io << ">" 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) @bytes.<=>(other.@bytes) end