From 8950315e53a23a0e9297bafb0889016b0d0b0d49 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Wed, 26 Feb 2025 13:18:24 +0200 Subject: [PATCH 1/2] Fix transient specs for Time.new Fix building a new Time instance with naively increased hours by 1: ```ruby Time.new(..., t.hour + 1, ...) ``` It leads to hours overflowing (becoming greater than 23) and transient failures like the following ones: ``` 1) Time.now Timezone object returned value by #utc_to_local and #local_to_utc methods could be Time instance ERROR ArgumentError: argument out of range 2) Time.now Timezone object returned value by #utc_to_local and #local_to_utc methods could be Time subclass instance ERROR ArgumentError: argument out of range ``` --- spec/ruby/core/time/now_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/ruby/core/time/now_spec.rb b/spec/ruby/core/time/now_spec.rb index 53cfa555f4f..3db5bf46a68 100644 --- a/spec/ruby/core/time/now_spec.rb +++ b/spec/ruby/core/time/now_spec.rb @@ -114,7 +114,8 @@ def zone.utc_to_local(time) it "could be Time instance" do zone = Object.new def zone.utc_to_local(t) - Time.new(t.year, t.mon, t.day, t.hour + 1, t.min, t.sec, t.utc_offset) + time = Time.new(t.year, t.mon, t.day, t.hour, t.min, t.sec, t.utc_offset) + time + 60 * 60 # + 1 hour end Time.now(in: zone).should be_kind_of(Time) @@ -124,7 +125,10 @@ def zone.utc_to_local(t) it "could be Time subclass instance" do zone = Object.new def zone.utc_to_local(t) - Class.new(Time).new(t.year, t.mon, t.day, t.hour + 1, t.min, t.sec, t.utc_offset) + time = Time.new(t.year, t.mon, t.day, t.hour, t.min, t.sec, t.utc_offset) + time += 60 * 60 # + 1 hour + + Class.new(Time).new(time.year, time.mon, time.day, time.hour, time.min, time.sec, time.utc_offset) end Time.now(in: zone).should be_kind_of(Time) From 09fa947e8319a821738ccd0b987a020b393d40af Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Wed, 26 Feb 2025 13:41:15 +0200 Subject: [PATCH 2/2] Update Time-related specs to not overflow hours and days --- spec/ruby/core/time/new_spec.rb | 7 +++++-- spec/ruby/core/time/now_spec.rb | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/ruby/core/time/new_spec.rb b/spec/ruby/core/time/new_spec.rb index 59b13216ad9..47c8dd2444a 100644 --- a/spec/ruby/core/time/new_spec.rb +++ b/spec/ruby/core/time/new_spec.rb @@ -243,7 +243,8 @@ def zone.local_to_utc(time) it "could be Time instance" do zone = Object.new def zone.local_to_utc(t) - Time.utc(t.year, t.mon, t.day, t.hour - 1, t.min, t.sec) + time = Time.utc(t.year, t.mon, t.day, t.hour, t.min, t.sec) + time - 60 * 60 # - 1 hour end Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time) @@ -253,7 +254,9 @@ def zone.local_to_utc(t) it "could be Time subclass instance" do zone = Object.new def zone.local_to_utc(t) - Class.new(Time).utc(t.year, t.mon, t.day, t.hour - 1, t.min, t.sec) + time = Time.utc(t.year, t.mon, t.day, t.hour, t.min, t.sec) + time -= 60 * 60 # - 1 hour + Class.new(Time).utc(time.year, time.mon, time.day, time.hour, t.min, t.sec) end Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time) diff --git a/spec/ruby/core/time/now_spec.rb b/spec/ruby/core/time/now_spec.rb index 3db5bf46a68..fd0f60b5ccc 100644 --- a/spec/ruby/core/time/now_spec.rb +++ b/spec/ruby/core/time/now_spec.rb @@ -170,7 +170,9 @@ def zone.utc_to_local(t) it "raises ArgumentError if difference between argument and result is too large" do zone = Object.new def zone.utc_to_local(t) - Time.utc(t.year, t.mon, t.day - 1, t.hour, t.min, t.sec, t.utc_offset) + time = Time.utc(t.year, t.mon, t.day, t.hour, t.min, t.sec, t.utc_offset) + time -= 24 * 60 * 60 # - 1 day + Time.utc(time.year, time.mon, time.day, time.hour, time.min, time.sec, time.utc_offset) end -> {