Skip to content

Commit 8950315

Browse files
committed
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 ```
1 parent 87fe4ae commit 8950315

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

spec/ruby/core/time/now_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def zone.utc_to_local(time)
114114
it "could be Time instance" do
115115
zone = Object.new
116116
def zone.utc_to_local(t)
117-
Time.new(t.year, t.mon, t.day, t.hour + 1, t.min, t.sec, t.utc_offset)
117+
time = Time.new(t.year, t.mon, t.day, t.hour, t.min, t.sec, t.utc_offset)
118+
time + 60 * 60 # + 1 hour
118119
end
119120

120121
Time.now(in: zone).should be_kind_of(Time)
@@ -124,7 +125,10 @@ def zone.utc_to_local(t)
124125
it "could be Time subclass instance" do
125126
zone = Object.new
126127
def zone.utc_to_local(t)
127-
Class.new(Time).new(t.year, t.mon, t.day, t.hour + 1, t.min, t.sec, t.utc_offset)
128+
time = Time.new(t.year, t.mon, t.day, t.hour, t.min, t.sec, t.utc_offset)
129+
time += 60 * 60 # + 1 hour
130+
131+
Class.new(Time).new(time.year, time.mon, time.day, time.hour, time.min, time.sec, time.utc_offset)
128132
end
129133

130134
Time.now(in: zone).should be_kind_of(Time)

0 commit comments

Comments
 (0)