From 02a5eed28a93f861b7a34d8911d2e01a469b9ffb Mon Sep 17 00:00:00 2001 From: Davor Ocelic Date: Thu, 21 Dec 2023 08:04:48 +0100 Subject: [PATCH] Improve comparison to Times --- shard.yml | 2 +- src/virtualtime.cr | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/shard.yml b/shard.yml index 5c52d90..84d260e 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: virtualtime -version: 1.1.5 +version: 1.1.6 authors: - Davor Ocelic diff --git a/src/virtualtime.cr b/src/virtualtime.cr index b6a6d41..2d41691 100644 --- a/src/virtualtime.cr +++ b/src/virtualtime.cr @@ -7,7 +7,7 @@ end class VirtualTime VERSION_MAJOR = 1 VERSION_MINOR = 1 - VERSION_REVISION = 5 + VERSION_REVISION = 6 VERSION = [VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION].join '.' include Comparable(Time) @@ -330,29 +330,30 @@ class VirtualTime end # :ditto: - def self.materialize(value : Enumerable(Int), default : Int32, max = nil, strict = true) - if max && value.any?(&.<(0)) - value = value.map { |e| e < 0 ? max + e + 1 : e } + def self.materialize(value : Range(Int, Int), default : Int32, max = nil, strict = true) + if max && (value.begin < 0 || value.end < 0) + ab = value.begin < 0 ? max + value.begin + 1 : value.begin + ae = value.end < 0 ? max + value.end + 1 : value.end + value = ab..ae end + if !strict || value.includes? default default else - value.min + value.begin end end # :ditto: - def self.materialize(value : Range(Int, Int), default : Int32, max = nil, strict = true) - if max && (value.begin < 0 || value.end < 0) - ab = value.begin < 0 ? max + value.begin + 1 : value.begin - ae = value.end < 0 ? max + value.end + 1 : value.end - value = ab..ae + def self.materialize(value : Enumerable(Int), default : Int32, max = nil, strict = true) + value = value.dup.to_a + if max && value.any?(&.<(0)) + value = value.map { |e| e < 0 ? max + e + 1 : e } end - if !strict || value.includes? default default else - value.begin + value.min end end @@ -381,7 +382,7 @@ class VirtualTime # Compares `VirtualTime` to `Time` instance def <=>(other : Time) - to_time <=> other + to_time(other) <=> other end # "Rewinds" `day` forward enough to reach `acceptable_day`.