diff options
Diffstat (limited to 'spec/ruby/core/time')
26 files changed, 739 insertions, 698 deletions
diff --git a/spec/ruby/core/time/_dump_spec.rb b/spec/ruby/core/time/_dump_spec.rb index 4dc1c43cd2..21f0806327 100644 --- a/spec/ruby/core/time/_dump_spec.rb +++ b/spec/ruby/core/time/_dump_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' describe "Time#_dump" do @@ -10,7 +10,7 @@ describe "Time#_dump" do end it "is a private method" do - Time.should have_private_instance_method(:_dump, false) + Time.private_instance_methods(false).should.include?(:_dump) end # http://redmine.ruby-lang.org/issues/show/627 @@ -25,7 +25,7 @@ describe "Time#_dump" do end it "dumps a Time object to a bytestring" do - @s.should be_an_instance_of(String) + @s.should.instance_of?(String) @s.should == [3222863947, 2235564032].pack("VV") end diff --git a/spec/ruby/core/time/_load_spec.rb b/spec/ruby/core/time/_load_spec.rb index bb0d705bbc..a74e3dc129 100644 --- a/spec/ruby/core/time/_load_spec.rb +++ b/spec/ruby/core/time/_load_spec.rb @@ -1,9 +1,9 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' describe "Time._load" do it "is a private method" do - Time.should have_private_method(:_load, false) + Time.private_methods(false).should.include?(:_load) end # http://redmine.ruby-lang.org/issues/show/627 diff --git a/spec/ruby/core/time/at_spec.rb b/spec/ruby/core/time/at_spec.rb index 85bb6d7ebf..10d4d36a68 100644 --- a/spec/ruby/core/time/at_spec.rb +++ b/spec/ruby/core/time/at_spec.rb @@ -20,7 +20,7 @@ describe "Time.at" do it "returns a subclass instance on a Time subclass" do c = Class.new(Time) t = c.at(0) - t.should be_an_instance_of(c) + t.should.instance_of?(c) end it "roundtrips a Rational produced by #to_r" do @@ -56,7 +56,7 @@ describe "Time.at" do it "creates a dup time object with the value given by time" do t1 = Time.new t2 = Time.at(t1) - t1.should_not equal t2 + t1.should_not.equal? t2 end it "returns a UTC time if the argument is UTC" do @@ -72,17 +72,17 @@ describe "Time.at" do it "returns a subclass instance" do c = Class.new(Time) t = c.at(Time.now) - t.should be_an_instance_of(c) + t.should.instance_of?(c) end end describe "passed non-Time, non-Numeric" do it "raises a TypeError with a String argument" do - -> { Time.at("0") }.should raise_error(TypeError) + -> { Time.at("0") }.should.raise(TypeError) end it "raises a TypeError with a nil argument" do - -> { Time.at(nil) }.should raise_error(TypeError) + -> { Time.at(nil) }.should.raise(TypeError) end describe "with an argument that responds to #to_int" do @@ -102,8 +102,8 @@ describe "Time.at" do it "needs for the argument to respond to #to_int too" do o = mock('rational-but-no-to_int') - o.should_receive(:to_r).and_return(Rational(5, 2)) - -> { Time.at(o) }.should raise_error(TypeError) + def o.to_r; Rational(5, 2) end + -> { Time.at(o) }.should.raise(TypeError, "can't convert MockObject into an exact number") end end end @@ -140,20 +140,20 @@ describe "Time.at" do describe "passed [Integer, nil]" do it "raises a TypeError" do - -> { Time.at(0, nil) }.should raise_error(TypeError) + -> { Time.at(0, nil) }.should.raise(TypeError) end end describe "passed [Integer, String]" do it "raises a TypeError" do - -> { Time.at(0, "0") }.should raise_error(TypeError) + -> { Time.at(0, "0") }.should.raise(TypeError) end end describe "passed [Time, Integer]" do # #8173 it "raises a TypeError" do - -> { Time.at(Time.now, 500000) }.should raise_error(TypeError) + -> { Time.at(Time.now, 500000) }.should.raise(TypeError) end end @@ -190,15 +190,15 @@ describe "Time.at" do context "not supported format" do it "raises ArgumentError" do - -> { Time.at(0, 123456, 2) }.should raise_error(ArgumentError) - -> { Time.at(0, 123456, nil) }.should raise_error(ArgumentError) - -> { Time.at(0, 123456, :invalid) }.should raise_error(ArgumentError) + -> { Time.at(0, 123456, 2) }.should.raise(ArgumentError) + -> { Time.at(0, 123456, nil) }.should.raise(ArgumentError) + -> { Time.at(0, 123456, :invalid) }.should.raise(ArgumentError) end it "does not try to convert format to Symbol with #to_sym" do format = +"usec" format.should_not_receive(:to_sym) - -> { Time.at(0, 123456, format) }.should raise_error(ArgumentError) + -> { Time.at(0, 123456, format) }.should.raise(ArgumentError) end end @@ -283,53 +283,33 @@ describe "Time.at" do end it "raises ArgumentError if format is invalid" do - -> { Time.at(@epoch_time, in: "+09:99") }.should raise_error(ArgumentError) - -> { Time.at(@epoch_time, in: "ABC") }.should raise_error(ArgumentError) + -> { Time.at(@epoch_time, in: "+09:99") }.should.raise(ArgumentError) + -> { Time.at(@epoch_time, in: "ABC") }.should.raise(ArgumentError) end it "raises ArgumentError if hours greater than 23" do # TODO - ruby_version_is ""..."3.1" do - -> { Time.at(@epoch_time, in: "+24:00") }.should raise_error(ArgumentError, 'utc_offset out of range') - -> { Time.at(@epoch_time, in: "+2400") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - - -> { Time.at(@epoch_time, in: "+99:00") }.should raise_error(ArgumentError, 'utc_offset out of range') - -> { Time.at(@epoch_time, in: "+9900") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - end + -> { Time.at(@epoch_time, in: "+24:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.at(@epoch_time, in: "+2400") }.should.raise(ArgumentError, "utc_offset out of range") - ruby_version_is "3.1" do - -> { Time.at(@epoch_time, in: "+24:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.at(@epoch_time, in: "+2400") }.should raise_error(ArgumentError, "utc_offset out of range") - - -> { Time.at(@epoch_time, in: "+99:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.at(@epoch_time, in: "+9900") }.should raise_error(ArgumentError, "utc_offset out of range") - end + -> { Time.at(@epoch_time, in: "+99:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.at(@epoch_time, in: "+9900") }.should.raise(ArgumentError, "utc_offset out of range") end it "raises ArgumentError if minutes greater than 59" do # TODO - ruby_version_is ""..."3.1" do - -> { Time.at(@epoch_time, in: "+00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - -> { Time.at(@epoch_time, in: "+0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') + -> { Time.at(@epoch_time, in: "+00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') + -> { Time.at(@epoch_time, in: "+0060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') - -> { Time.at(@epoch_time, in: "+00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - -> { Time.at(@epoch_time, in: "+0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - end - - ruby_version_is "3.1" do - -> { Time.at(@epoch_time, in: "+00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') - -> { Time.at(@epoch_time, in: "+0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') - - -> { Time.at(@epoch_time, in: "+00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') - -> { Time.at(@epoch_time, in: "+0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') - end + -> { Time.at(@epoch_time, in: "+00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') + -> { Time.at(@epoch_time, in: "+0099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') end ruby_bug '#20797', ''...'3.4' do it "raises ArgumentError if seconds greater than 59" do - -> { Time.at(@epoch_time, in: "+00:00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') - -> { Time.at(@epoch_time, in: "+000060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') + -> { Time.at(@epoch_time, in: "+00:00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') + -> { Time.at(@epoch_time, in: "+000060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') - -> { Time.at(@epoch_time, in: "+00:00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') - -> { Time.at(@epoch_time, in: "+000099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') + -> { Time.at(@epoch_time, in: "+00:00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') + -> { Time.at(@epoch_time, in: "+000099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') end end end diff --git a/spec/ruby/core/time/ceil_spec.rb b/spec/ruby/core/time/ceil_spec.rb index 9d624a1ed0..18e26f9994 100644 --- a/spec/ruby/core/time/ceil_spec.rb +++ b/spec/ruby/core/time/ceil_spec.rb @@ -28,8 +28,8 @@ describe "Time#ceil" do it "returns an instance of Time, even if #ceil is called on a subclass" do subclass = Class.new(Time) instance = subclass.at(0) - instance.class.should equal subclass - instance.ceil.should be_an_instance_of(Time) + instance.class.should.equal? subclass + instance.ceil.should.instance_of?(Time) end it "copies own timezone to the returning value" do diff --git a/spec/ruby/core/time/comparison_spec.rb b/spec/ruby/core/time/comparison_spec.rb index 866fbea72e..0790088f9e 100644 --- a/spec/ruby/core/time/comparison_spec.rb +++ b/spec/ruby/core/time/comparison_spec.rb @@ -124,7 +124,7 @@ describe "Time#<=>" do def r.<=>(other); other <=> self; end r.should_receive(:<=>).once - (t <=> r).should be_nil + (t <=> r).should == nil end end end diff --git a/spec/ruby/core/time/deconstruct_keys_spec.rb b/spec/ruby/core/time/deconstruct_keys_spec.rb index ee17e7dbd4..9918728c1d 100644 --- a/spec/ruby/core/time/deconstruct_keys_spec.rb +++ b/spec/ruby/core/time/deconstruct_keys_spec.rb @@ -1,45 +1,43 @@ require_relative '../../spec_helper' -ruby_version_is "3.2" do - describe "Time#deconstruct_keys" do - it "returns whole hash for nil as an argument" do - d = Time.utc(2022, 10, 5, 13, 30) - res = { year: 2022, month: 10, day: 5, yday: 278, wday: 3, hour: 13, - min: 30, sec: 0, subsec: 0, dst: false, zone: "UTC" } - d.deconstruct_keys(nil).should == res - end - - it "returns only specified keys" do - d = Time.utc(2022, 10, 5, 13, 39) - d.deconstruct_keys([:zone, :subsec]).should == { zone: "UTC", subsec: 0 } - end - - it "requires one argument" do - -> { - Time.new(2022, 10, 5, 13, 30).deconstruct_keys - }.should raise_error(ArgumentError) - end - - it "it raises error when argument is neither nil nor array" do - d = Time.new(2022, 10, 5, 13, 30) - - -> { d.deconstruct_keys(1) }.should raise_error(TypeError, "wrong argument type Integer (expected Array or nil)") - -> { d.deconstruct_keys("asd") }.should raise_error(TypeError, "wrong argument type String (expected Array or nil)") - -> { d.deconstruct_keys(:x) }.should raise_error(TypeError, "wrong argument type Symbol (expected Array or nil)") - -> { d.deconstruct_keys({}) }.should raise_error(TypeError, "wrong argument type Hash (expected Array or nil)") - end - - it "returns {} when passed []" do - Time.new(2022, 10, 5, 13, 30).deconstruct_keys([]).should == {} - end - - it "ignores non-Symbol keys" do - Time.new(2022, 10, 5, 13, 30).deconstruct_keys(['year', []]).should == {} - end - - it "ignores not existing Symbol keys and processes keys after the first non-existing one" do - d = Time.utc(2022, 10, 5, 13, 30) - d.deconstruct_keys([:year, :a, :month, :b, :day]).should == { year: 2022, month: 10, day: 5 } - end +describe "Time#deconstruct_keys" do + it "returns whole hash for nil as an argument" do + d = Time.utc(2022, 10, 5, 13, 30) + res = { year: 2022, month: 10, day: 5, yday: 278, wday: 3, hour: 13, + min: 30, sec: 0, subsec: 0, dst: false, zone: "UTC" } + d.deconstruct_keys(nil).should == res + end + + it "returns only specified keys" do + d = Time.utc(2022, 10, 5, 13, 39) + d.deconstruct_keys([:zone, :subsec]).should == { zone: "UTC", subsec: 0 } + end + + it "requires one argument" do + -> { + Time.new(2022, 10, 5, 13, 30).deconstruct_keys + }.should.raise(ArgumentError) + end + + it "it raises error when argument is neither nil nor array" do + d = Time.new(2022, 10, 5, 13, 30) + + -> { d.deconstruct_keys(1) }.should.raise(TypeError, "wrong argument type Integer (expected Array or nil)") + -> { d.deconstruct_keys("asd") }.should.raise(TypeError, "wrong argument type String (expected Array or nil)") + -> { d.deconstruct_keys(:x) }.should.raise(TypeError, "wrong argument type Symbol (expected Array or nil)") + -> { d.deconstruct_keys({}) }.should.raise(TypeError, "wrong argument type Hash (expected Array or nil)") + end + + it "returns {} when passed []" do + Time.new(2022, 10, 5, 13, 30).deconstruct_keys([]).should == {} + end + + it "ignores non-Symbol keys" do + Time.new(2022, 10, 5, 13, 30).deconstruct_keys(['year', []]).should == {} + end + + it "ignores not existing Symbol keys and processes keys after the first non-existing one" do + d = Time.utc(2022, 10, 5, 13, 30) + d.deconstruct_keys([:year, :a, :month, :b, :day]).should == { year: 2022, month: 10, day: 5 } end end diff --git a/spec/ruby/core/time/dup_spec.rb b/spec/ruby/core/time/dup_spec.rb index 5d6621beaa..33aa1304ef 100644 --- a/spec/ruby/core/time/dup_spec.rb +++ b/spec/ruby/core/time/dup_spec.rb @@ -22,25 +22,25 @@ describe "Time#dup" do c = Class.new(Time) t = c.now - t.should be_an_instance_of(c) - t.dup.should be_an_instance_of(c) + t.should.instance_of?(c) + t.dup.should.instance_of?(c) end it "returns a clone of Time instance" do c = Time.dup t = c.now - t.should be_an_instance_of(c) - t.should_not be_an_instance_of(Time) + t.should.instance_of?(c) + t.should_not.instance_of?(Time) - t.dup.should be_an_instance_of(c) - t.dup.should_not be_an_instance_of(Time) + t.dup.should.instance_of?(c) + t.dup.should_not.instance_of?(Time) end it "does not copy frozen status from the original" do t = Time.now t.freeze t2 = t.dup - t2.frozen?.should be_false + t2.frozen?.should == false end end diff --git a/spec/ruby/core/time/eql_spec.rb b/spec/ruby/core/time/eql_spec.rb index 2ffb4eec96..b7505969dd 100644 --- a/spec/ruby/core/time/eql_spec.rb +++ b/spec/ruby/core/time/eql_spec.rb @@ -2,28 +2,28 @@ require_relative '../../spec_helper' describe "Time#eql?" do it "returns true if self and other have the same whole number of seconds" do - Time.at(100).should eql(Time.at(100)) + Time.at(100).should.eql?(Time.at(100)) end it "returns false if self and other have differing whole numbers of seconds" do - Time.at(100).should_not eql(Time.at(99)) + Time.at(100).should_not.eql?(Time.at(99)) end it "returns true if self and other have the same number of microseconds" do - Time.at(100, 100).should eql(Time.at(100, 100)) + Time.at(100, 100).should.eql?(Time.at(100, 100)) end it "returns false if self and other have differing numbers of microseconds" do - Time.at(100, 100).should_not eql(Time.at(100, 99)) + Time.at(100, 100).should_not.eql?(Time.at(100, 99)) end it "returns false if self and other have differing fractional microseconds" do - Time.at(100, Rational(100,1000)).should_not eql(Time.at(100, Rational(99,1000))) + Time.at(100, Rational(100,1000)).should_not.eql?(Time.at(100, Rational(99,1000))) end it "returns false when given a non-time value" do - Time.at(100, 100).should_not eql("100") - Time.at(100, 100).should_not eql(100) - Time.at(100, 100).should_not eql(100.1) + Time.at(100, 100).should_not.eql?("100") + Time.at(100, 100).should_not.eql?(100) + Time.at(100, 100).should_not.eql?(100.1) end end diff --git a/spec/ruby/core/time/floor_spec.rb b/spec/ruby/core/time/floor_spec.rb index b0003469c9..41e5142b19 100644 --- a/spec/ruby/core/time/floor_spec.rb +++ b/spec/ruby/core/time/floor_spec.rb @@ -20,8 +20,8 @@ describe "Time#floor" do it "returns an instance of Time, even if #floor is called on a subclass" do subclass = Class.new(Time) instance = subclass.at(0) - instance.class.should equal subclass - instance.floor.should be_an_instance_of(Time) + instance.class.should.equal? subclass + instance.floor.should.instance_of?(Time) end it "copies own timezone to the returning value" do diff --git a/spec/ruby/core/time/getlocal_spec.rb b/spec/ruby/core/time/getlocal_spec.rb index ff3e3d8dd1..7e5334c303 100644 --- a/spec/ruby/core/time/getlocal_spec.rb +++ b/spec/ruby/core/time/getlocal_spec.rb @@ -14,6 +14,7 @@ describe "Time#getlocal" do t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(3630) t.should == Time.new(2007, 1, 9, 13, 0, 30, 3630) t.utc_offset.should == 3630 + t.zone.should == nil end platform_is_not :windows do @@ -40,7 +41,7 @@ describe "Time#getlocal" do it "returns a Time with a UTC offset of the specified number of Rational seconds" do t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(Rational(7201, 2)) t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2)) - t.utc_offset.should eql(Rational(7201, 2)) + t.utc_offset.should.eql?(Rational(7201, 2)) end describe "with an argument that responds to #to_r" do @@ -49,7 +50,7 @@ describe "Time#getlocal" do o.should_receive(:to_r).and_return(Rational(7201, 2)) t = Time.gm(2007, 1, 9, 12, 0, 0).getlocal(o) t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2)) - t.utc_offset.should eql(Rational(7201, 2)) + t.utc_offset.should.eql?(Rational(7201, 2)) end end @@ -89,69 +90,49 @@ describe "Time#getlocal" do it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do t = Time.now - -> { t.getlocal("3600") }.should raise_error(ArgumentError) + -> { t.getlocal("3600") }.should.raise(ArgumentError) end it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do t = Time.now - -> { t.getlocal("-01:00".encode("UTF-16LE")) }.should raise_error(ArgumentError) + -> { t.getlocal("-01:00".encode("UTF-16LE")) }.should.raise(ArgumentError) end it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do t = Time.new t.getlocal(-86400 + 1).utc_offset.should == (-86400 + 1) - -> { t.getlocal(-86400) }.should raise_error(ArgumentError) + -> { t.getlocal(-86400) }.should.raise(ArgumentError) end it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do t = Time.new t.getlocal(86400 - 1).utc_offset.should == (86400 - 1) - -> { t.getlocal(86400) }.should raise_error(ArgumentError) + -> { t.getlocal(86400) }.should.raise(ArgumentError) end it "raises ArgumentError if String argument and hours greater than 23" do - ruby_version_is ""..."3.1" do - -> { Time.now.getlocal("+24:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.getlocal("+2400") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') + -> { Time.now.getlocal("+24:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.now.getlocal("+2400") }.should.raise(ArgumentError, "utc_offset out of range") - -> { Time.now.getlocal("+99:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.getlocal("+9900") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - end - - ruby_version_is "3.1" do - -> { Time.now.getlocal("+24:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.getlocal("+2400") }.should raise_error(ArgumentError, "utc_offset out of range") - - -> { Time.now.getlocal("+99:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.getlocal("+9900") }.should raise_error(ArgumentError, "utc_offset out of range") - end + -> { Time.now.getlocal("+99:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.now.getlocal("+9900") }.should.raise(ArgumentError, "utc_offset out of range") end it "raises ArgumentError if String argument and minutes greater than 59" do - ruby_version_is ""..."3.1" do - -> { Time.now.getlocal("+00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - -> { Time.now.getlocal("+0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') + -> { Time.now.getlocal("+00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') + -> { Time.now.getlocal("+0060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') - -> { Time.now.getlocal("+00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - -> { Time.now.getlocal("+0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - end - - ruby_version_is "3.1" do - -> { Time.now.getlocal("+00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') - -> { Time.now.getlocal("+0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') - - -> { Time.now.getlocal("+00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') - -> { Time.now.getlocal("+0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') - end + -> { Time.now.getlocal("+00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') + -> { Time.now.getlocal("+0099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') end ruby_bug '#20797', ''...'3.4' do it "raises ArgumentError if String argument and seconds greater than 59" do - -> { Time.now.getlocal("+00:00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') - -> { Time.now.getlocal("+000060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') + -> { Time.now.getlocal("+00:00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') + -> { Time.now.getlocal("+000060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') - -> { Time.now.getlocal("+00:00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') - -> { Time.now.getlocal("+000099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') + -> { Time.now.getlocal("+00:00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') + -> { Time.now.getlocal("+000099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') end end @@ -174,8 +155,8 @@ describe "Time#getlocal" do end -> { - Time.utc(2000, 1, 1, 12, 0, 0).getlocal(zone).should be_kind_of(Time) - }.should_not raise_error + Time.utc(2000, 1, 1, 12, 0, 0).getlocal(zone).should.is_a?(Time) + }.should_not.raise end it "raises TypeError if timezone does not implement #utc_to_local method" do @@ -186,7 +167,7 @@ describe "Time#getlocal" do -> { Time.utc(2000, 1, 1, 12, 0, 0).getlocal(zone) - }.should raise_error(TypeError, /can't convert \w+ into an exact number/) + }.should.raise(TypeError, /can't convert \w+ into an exact number/) end it "does not raise exception if timezone does not implement #local_to_utc method" do @@ -196,18 +177,18 @@ describe "Time#getlocal" do end -> { - Time.utc(2000, 1, 1, 12, 0, 0).getlocal(zone).should be_kind_of(Time) - }.should_not raise_error + Time.utc(2000, 1, 1, 12, 0, 0).getlocal(zone).should.is_a?(Time) + }.should_not.raise end context "subject's class implements .find_timezone method" do it "calls .find_timezone to build a time object if passed zone name as a timezone argument" do time = TimeSpecs::TimeWithFindTimezone.utc(2000, 1, 1, 12, 0, 0).getlocal("Asia/Colombo") - time.zone.should be_kind_of TimeSpecs::TimezoneWithName + time.zone.should.is_a? TimeSpecs::TimezoneWithName time.zone.name.should == "Asia/Colombo" time = TimeSpecs::TimeWithFindTimezone.utc(2000, 1, 1, 12, 0, 0).getlocal("some invalid zone name") - time.zone.should be_kind_of TimeSpecs::TimezoneWithName + time.zone.should.is_a? TimeSpecs::TimezoneWithName time.zone.name.should == "some invalid zone name" end @@ -217,7 +198,7 @@ describe "Time#getlocal" do -> { time.getlocal(zone) - }.should raise_error(TypeError, /can't convert \w+ into an exact number/) + }.should.raise(TypeError, /can't convert \w+ into an exact number/) end end end diff --git a/spec/ruby/core/time/hash_spec.rb b/spec/ruby/core/time/hash_spec.rb index 4f4d11a2cd..1cfc56eab0 100644 --- a/spec/ruby/core/time/hash_spec.rb +++ b/spec/ruby/core/time/hash_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "Time#hash" do it "returns an Integer" do - Time.at(100).hash.should be_an_instance_of(Integer) + Time.at(100).hash.should.instance_of?(Integer) end it "is stable" do diff --git a/spec/ruby/core/time/localtime_spec.rb b/spec/ruby/core/time/localtime_spec.rb index 5badba9fb2..1c0b11b7a6 100644 --- a/spec/ruby/core/time/localtime_spec.rb +++ b/spec/ruby/core/time/localtime_spec.rb @@ -12,7 +12,7 @@ describe "Time#localtime" do it "returns self" do t = Time.gm(2007, 1, 9, 12, 0, 0) - t.localtime.should equal(t) + t.localtime.should.equal?(t) end it "converts time to the UTC offset specified as an Integer number of seconds" do @@ -26,13 +26,13 @@ describe "Time#localtime" do it "does not raise an error if already in the right time zone" do time = Time.now time.freeze - time.localtime.should equal(time) + time.localtime.should.equal?(time) end it "raises a FrozenError if the time has a different time zone" do time = Time.gm(2007, 1, 9, 12, 0, 0) time.freeze - -> { time.localtime }.should raise_error(FrozenError) + -> { time.localtime }.should.raise(FrozenError) end end @@ -51,7 +51,7 @@ describe "Time#localtime" do t = Time.gm(2007, 1, 9, 12, 0, 0) t.localtime(Rational(7201, 2)) t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2)) - t.utc_offset.should eql(Rational(7201, 2)) + t.utc_offset.should.eql?(Rational(7201, 2)) end describe "with an argument that responds to #to_r" do @@ -61,7 +61,7 @@ describe "Time#localtime" do t = Time.gm(2007, 1, 9, 12, 0, 0) t.localtime(o) t.should == Time.new(2007, 1, 9, 13, 0, Rational(1, 2), Rational(7201, 2)) - t.utc_offset.should eql(Rational(7201, 2)) + t.utc_offset.should.eql?(Rational(7201, 2)) end end @@ -106,48 +106,28 @@ describe "Time#localtime" do end it "raises ArgumentError if String argument and hours greater than 23" do - ruby_version_is ""..."3.1" do - -> { Time.now.localtime("+24:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.localtime("+2400") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') + -> { Time.now.localtime("+24:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.now.localtime("+2400") }.should.raise(ArgumentError, "utc_offset out of range") - -> { Time.now.localtime("+99:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.localtime("+9900") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - end - - ruby_version_is "3.1" do - -> { Time.now.localtime("+24:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.localtime("+2400") }.should raise_error(ArgumentError, "utc_offset out of range") - - -> { Time.now.localtime("+99:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now.localtime("+9900") }.should raise_error(ArgumentError, "utc_offset out of range") - end + -> { Time.now.localtime("+99:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.now.localtime("+9900") }.should.raise(ArgumentError, "utc_offset out of range") end it "raises ArgumentError if String argument and minutes greater than 59" do - ruby_version_is ""..."3.1" do - -> { Time.now.localtime("+00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - -> { Time.now.localtime("+0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') + -> { Time.now.localtime("+00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') + -> { Time.now.localtime("+0060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') - -> { Time.now.localtime("+00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - -> { Time.now.localtime("+0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset') - end - - ruby_version_is "3.1" do - -> { Time.now.localtime("+00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') - -> { Time.now.localtime("+0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') - - -> { Time.now.localtime("+00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') - -> { Time.now.localtime("+0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') - end + -> { Time.now.localtime("+00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') + -> { Time.now.localtime("+0099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') end ruby_bug '#20797', ''...'3.4' do it "raises ArgumentError if String argument and seconds greater than 59" do - -> { Time.now.localtime("+00:00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') - -> { Time.now.localtime("+000060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') + -> { Time.now.localtime("+00:00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') + -> { Time.now.localtime("+000060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') - -> { Time.now.localtime("+00:00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') - -> { Time.now.localtime("+000099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') + -> { Time.now.localtime("+00:00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') + -> { Time.now.localtime("+000099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') end end @@ -188,25 +168,36 @@ describe "Time#localtime" do end end + describe "with an argument that responds to #utc_to_local" do + it "coerces using #utc_to_local" do + o = mock('string') + o.should_receive(:utc_to_local).and_return(Time.new(2007, 1, 9, 13, 0, 0, 3600)) + t = Time.gm(2007, 1, 9, 12, 0, 0) + t.localtime(o) + t.should == Time.new(2007, 1, 9, 13, 0, 0, 3600) + t.utc_offset.should == 3600 + end + end + it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do t = Time.now - -> { t.localtime("3600") }.should raise_error(ArgumentError) + -> { t.localtime("3600") }.should.raise(ArgumentError) end it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do t = Time.now - -> { t.localtime("-01:00".encode("UTF-16LE")) }.should raise_error(ArgumentError) + -> { t.localtime("-01:00".encode("UTF-16LE")) }.should.raise(ArgumentError) end it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do t = Time.new t.localtime(-86400 + 1).utc_offset.should == (-86400 + 1) - -> { t.localtime(-86400) }.should raise_error(ArgumentError) + -> { t.localtime(-86400) }.should.raise(ArgumentError) end it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do t = Time.new t.localtime(86400 - 1).utc_offset.should == (86400 - 1) - -> { t.localtime(86400) }.should raise_error(ArgumentError) + -> { t.localtime(86400) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/time/minus_spec.rb b/spec/ruby/core/time/minus_spec.rb index 8449778465..ee3d8acda8 100644 --- a/spec/ruby/core/time/minus_spec.rb +++ b/spec/ruby/core/time/minus_spec.rb @@ -20,18 +20,18 @@ describe "Time#-" do end it "raises a TypeError if given argument is a coercible String" do - -> { Time.now - "1" }.should raise_error(TypeError) - -> { Time.now - "0.1" }.should raise_error(TypeError) - -> { Time.now - "1/3" }.should raise_error(TypeError) + -> { Time.now - "1" }.should.raise(TypeError) + -> { Time.now - "0.1" }.should.raise(TypeError) + -> { Time.now - "1/3" }.should.raise(TypeError) end it "raises TypeError on argument that can't be coerced" do - -> { Time.now - Object.new }.should raise_error(TypeError) - -> { Time.now - "stuff" }.should raise_error(TypeError) + -> { Time.now - Object.new }.should.raise(TypeError) + -> { Time.now - "stuff" }.should.raise(TypeError) end it "raises TypeError on nil argument" do - -> { Time.now - nil }.should raise_error(TypeError) + -> { Time.now - nil }.should.raise(TypeError) end it "tracks microseconds" do @@ -109,8 +109,8 @@ describe "Time#-" do it "does not return a subclass instance" do c = Class.new(Time) - x = c.now + 1 - x.should be_an_instance_of(Time) + x = c.now - 1 + x.should.instance_of?(Time) end it "returns a time with nanoseconds precision between two time objects" do diff --git a/spec/ruby/core/time/new_spec.rb b/spec/ruby/core/time/new_spec.rb index 1a743b485e..91ce4b2e3a 100644 --- a/spec/ruby/core/time/new_spec.rb +++ b/spec/ruby/core/time/new_spec.rb @@ -31,14 +31,14 @@ describe "Time.new with a utc_offset argument" do end it "returns a Time with a UTC offset of the specified number of Rational seconds" do - Time.new(2000, 1, 1, 0, 0, 0, Rational(5, 2)).utc_offset.should eql(Rational(5, 2)) + Time.new(2000, 1, 1, 0, 0, 0, Rational(5, 2)).utc_offset.should.eql?(Rational(5, 2)) end describe "with an argument that responds to #to_r" do it "coerces using #to_r" do o = mock_numeric('rational') o.should_receive(:to_r).and_return(Rational(5, 2)) - Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should eql(Rational(5, 2)) + Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should.eql?(Rational(5, 2)) end end @@ -58,30 +58,28 @@ describe "Time.new with a utc_offset argument" do Time.new(2000, 1, 1, 0, 0, 0, "-04:10:43").utc_offset.should == -15043 end - ruby_bug '#13669', ''...'3.1' do - it "returns a Time with a UTC offset specified as +HH" do - Time.new(2000, 1, 1, 0, 0, 0, "+05").utc_offset.should == 3600 * 5 - end + it "returns a Time with a UTC offset specified as +HH" do + Time.new(2000, 1, 1, 0, 0, 0, "+05").utc_offset.should == 3600 * 5 + end - it "returns a Time with a UTC offset specified as -HH" do - Time.new(2000, 1, 1, 0, 0, 0, "-05").utc_offset.should == -3600 * 5 - end + it "returns a Time with a UTC offset specified as -HH" do + Time.new(2000, 1, 1, 0, 0, 0, "-05").utc_offset.should == -3600 * 5 + end - it "returns a Time with a UTC offset specified as +HHMM" do - Time.new(2000, 1, 1, 0, 0, 0, "+0530").utc_offset.should == 19800 - end + it "returns a Time with a UTC offset specified as +HHMM" do + Time.new(2000, 1, 1, 0, 0, 0, "+0530").utc_offset.should == 19800 + end - it "returns a Time with a UTC offset specified as -HHMM" do - Time.new(2000, 1, 1, 0, 0, 0, "-0530").utc_offset.should == -19800 - end + it "returns a Time with a UTC offset specified as -HHMM" do + Time.new(2000, 1, 1, 0, 0, 0, "-0530").utc_offset.should == -19800 + end - it "returns a Time with a UTC offset specified as +HHMMSS" do - Time.new(2000, 1, 1, 0, 0, 0, "+053037").utc_offset.should == 19837 - end + it "returns a Time with a UTC offset specified as +HHMMSS" do + Time.new(2000, 1, 1, 0, 0, 0, "+053037").utc_offset.should == 19837 + end - it "returns a Time with a UTC offset specified as -HHMMSS" do - Time.new(2000, 1, 1, 0, 0, 0, "-053037").utc_offset.should == -19837 - end + it "returns a Time with a UTC offset specified as -HHMMSS" do + Time.new(2000, 1, 1, 0, 0, 0, "-053037").utc_offset.should == -19837 end describe "with an argument that responds to #to_str" do @@ -129,18 +127,9 @@ describe "Time.new with a utc_offset argument" do end end - ruby_version_is ""..."3.1" do - it "raises ArgumentError if the string argument is J" do - message = '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset' - -> { Time.new(2000, 1, 1, 0, 0, 0, "J") }.should raise_error(ArgumentError, message) - end - end - - ruby_version_is "3.1" do - it "raises ArgumentError if the string argument is J" do - message = '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: J' - -> { Time.new(2000, 1, 1, 0, 0, 0, "J") }.should raise_error(ArgumentError, message) - end + it "raises ArgumentError if the string argument is J" do + message = '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: J' + -> { Time.new(2000, 1, 1, 0, 0, 0, "J") }.should.raise(ArgumentError, message) end it "returns a local Time if the argument is nil" do @@ -155,18 +144,18 @@ describe "Time.new with a utc_offset argument" do it "disallows a value for minutes greater than 59" do -> { Time.new(2000, 1, 1, 0, 0, 0, "+01:60") - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) -> { Time.new(2000, 1, 1, 0, 0, 0, "+01:99") - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do - -> { Time.new(2000, 1, 1, 0, 0, 0, "3600") }.should raise_error(ArgumentError) + -> { Time.new(2000, 1, 1, 0, 0, 0, "3600") }.should.raise(ArgumentError) end it "raises ArgumentError if the hour value is greater than 23" do - -> { Time.new(2000, 1, 1, 0, 0, 0, "+24:00") }.should raise_error(ArgumentError) + -> { Time.new(2000, 1, 1, 0, 0, 0, "+24:00") }.should.raise(ArgumentError) end it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do @@ -175,24 +164,25 @@ describe "Time.new with a utc_offset argument" do # - '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset' -> { Time.new(2000, 1, 1, 0, 0, 0, "-04:10".encode("UTF-16LE")) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do Time.new(2000, 1, 1, 0, 0, 0, -86400 + 1).utc_offset.should == (-86400 + 1) - -> { Time.new(2000, 1, 1, 0, 0, 0, -86400) }.should raise_error(ArgumentError) + -> { Time.new(2000, 1, 1, 0, 0, 0, -86400) }.should.raise(ArgumentError) end it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do Time.new(2000, 1, 1, 0, 0, 0, 86400 - 1).utc_offset.should == (86400 - 1) - -> { Time.new(2000, 1, 1, 0, 0, 0, 86400) }.should raise_error(ArgumentError) + -> { Time.new(2000, 1, 1, 0, 0, 0, 86400) }.should.raise(ArgumentError) end it "raises ArgumentError if the utc_offset argument is greater than or equal to 10e9" do - -> { Time.new(2000, 1, 1, 0, 0, 0, 1000000000) }.should raise_error(ArgumentError) + -> { Time.new(2000, 1, 1, 0, 0, 0, 1000000000) }.should.raise(ArgumentError) end end +# The method #local_to_utc is tested only here because Time.new is the only method that calls #local_to_utc. describe "Time.new with a timezone argument" do it "returns a Time in the timezone" do zone = TimeSpecs::Timezone.new(offset: (5*3600+30*60)) @@ -213,9 +203,7 @@ describe "Time.new with a timezone argument" do time end - -> { - Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time) - }.should_not raise_error + Time.new(2000, 1, 1, 12, 0, 0, zone).should.is_a?(Time) end it "raises TypeError if timezone does not implement #local_to_utc method" do @@ -226,7 +214,7 @@ describe "Time.new with a timezone argument" do -> { Time.new(2000, 1, 1, 12, 0, 0, zone) - }.should raise_error(TypeError, /can't convert \w+ into an exact number/) + }.should.raise(TypeError, /can't convert Object into an exact number/) end it "does not raise exception if timezone does not implement #utc_to_local method" do @@ -235,51 +223,48 @@ describe "Time.new with a timezone argument" do time end - -> { - Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time) - }.should_not raise_error + Time.new(2000, 1, 1, 12, 0, 0, zone).should.is_a?(Time) end # The result also should be a Time or Time-like object (not necessary to be the same class) - # The zone of the result is just ignored + # or respond to #to_int method. The zone of the result is just ignored. describe "returned value by #utc_to_local and #local_to_utc methods" do 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) - Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60 - }.should_not raise_error + Time.new(2000, 1, 1, 12, 0, 0, zone).should.is_a?(Time) + Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60 end 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) - Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60 - }.should_not raise_error + Time.new(2000, 1, 1, 12, 0, 0, zone).should.is_a?(Time) + Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60 end it "could be any object with #to_i method" do zone = Object.new def zone.local_to_utc(time) - Struct.new(:to_i).new(time.to_i - 60*60) + obj = Object.new + obj.singleton_class.define_method(:to_i) { time.to_i - 60*60 } + obj end - -> { - Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time) - Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60 - }.should_not raise_error + Time.new(2000, 1, 1, 12, 0, 0, zone).should.is_a?(Time) + Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60 end - it "could have any #zone and #utc_offset because they are ignored" do + it "could have any #zone and #utc_offset because they are ignored if it isn't an instance of Time" do zone = Object.new def zone.local_to_utc(time) Struct.new(:to_i, :zone, :utc_offset).new(time.to_i, 'America/New_York', -5*60*60) @@ -293,7 +278,15 @@ describe "Time.new with a timezone argument" do Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 0 end - it "leads to raising Argument error if difference between argument and result is too large" do + it "cannot have arbitrary #utc_offset if it is an instance of Time" do + zone = Object.new + def zone.local_to_utc(t) + Time.new(t.year, t.mon, t.mday, t.hour, t.min, t.sec, 9*60*60) + end + Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 9*60*60 + end + + it "raises ArgumentError if difference between argument and result is too large" do zone = Object.new def zone.local_to_utc(t) Time.utc(t.year, t.mon, t.day + 1, t.hour, t.min, t.sec) @@ -301,7 +294,7 @@ describe "Time.new with a timezone argument" do -> { Time.new(2000, 1, 1, 12, 0, 0, zone) - }.should raise_error(ArgumentError, "utc_offset out of range") + }.should.raise(ArgumentError, "utc_offset out of range") end end @@ -318,12 +311,9 @@ describe "Time.new with a timezone argument" do end it "implements subset of Time methods" do + # List only methods that are explicitly documented. [ - :year, :mon, :month, :mday, :hour, :min, :sec, - :tv_sec, :tv_usec, :usec, :tv_nsec, :nsec, :subsec, - :to_i, :to_f, :to_r, :+, :-, - :isdst, :dst?, :zone, :gmtoff, :gmt_offset, :utc_offset, :utc?, :gmt?, - :to_s, :inspect, :to_a, :to_time, + :year, :mon, :mday, :hour, :min, :sec, :to_i, :isdst ].each do |name| @obj.respond_to?(name).should == true end @@ -358,7 +348,7 @@ describe "Time.new with a timezone argument" do -> { Marshal.dump(time) - }.should raise_error(NoMethodError, /undefined method [`']name' for/) + }.should.raise(NoMethodError, /undefined method [`']name' for/) end end @@ -379,18 +369,18 @@ describe "Time.new with a timezone argument" do time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, zone) time_loaded = Marshal.load(Marshal.dump(time)) - time_loaded.zone.should be_kind_of TimeSpecs::TimezoneWithName + time_loaded.zone.should.is_a? TimeSpecs::TimezoneWithName time_loaded.zone.name.should == "Asia/Colombo" time_loaded.utc_offset.should == 5*3600+30*60 end it "calls .find_timezone to build a time object if passed zone name as a timezone argument" do time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, "Asia/Colombo") - time.zone.should be_kind_of TimeSpecs::TimezoneWithName + time.zone.should.is_a? TimeSpecs::TimezoneWithName time.zone.name.should == "Asia/Colombo" time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, "some invalid zone name") - time.zone.should be_kind_of TimeSpecs::TimezoneWithName + time.zone.should.is_a? TimeSpecs::TimezoneWithName time.zone.name.should == "some invalid zone name" end @@ -398,340 +388,352 @@ describe "Time.new with a timezone argument" do [Object.new, [], {}, :"some zone"].each do |zone| -> { TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, zone) - }.should raise_error(TypeError, /can't convert \w+ into an exact number/) + }.should.raise(TypeError, /can't convert \w+ into an exact number/) end end end - ruby_version_is '3.1' do # https://bugs.ruby-lang.org/issues/17485 - describe ":in keyword argument" do - it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do - time = Time.new(2000, 1, 1, 12, 0, 0, in: "+05:00") + describe ":in keyword argument" do + it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do + time = Time.new(2000, 1, 1, 12, 0, 0, in: "+05:00") - time.utc_offset.should == 5*60*60 - time.zone.should == nil + time.utc_offset.should == 5*60*60 + time.zone.should == nil - time = Time.new(2000, 1, 1, 12, 0, 0, in: "-09:00") + time = Time.new(2000, 1, 1, 12, 0, 0, in: "-09:00") - time.utc_offset.should == -9*60*60 - time.zone.should == nil + time.utc_offset.should == -9*60*60 + time.zone.should == nil - time = Time.new(2000, 1, 1, 12, 0, 0, in: "-09:00:01") + time = Time.new(2000, 1, 1, 12, 0, 0, in: "-09:00:01") - time.utc_offset.should == -(9*60*60 + 1) - time.zone.should == nil - end + time.utc_offset.should == -(9*60*60 + 1) + time.zone.should == nil + end - it "could be UTC offset as a number of seconds" do - time = Time.new(2000, 1, 1, 12, 0, 0, in: 5*60*60) + it "could be UTC offset as a number of seconds" do + time = Time.new(2000, 1, 1, 12, 0, 0, in: 5*60*60) - time.utc_offset.should == 5*60*60 - time.zone.should == nil + time.utc_offset.should == 5*60*60 + time.zone.should == nil - time = Time.new(2000, 1, 1, 12, 0, 0, in: -9*60*60) + time = Time.new(2000, 1, 1, 12, 0, 0, in: -9*60*60) - time.utc_offset.should == -9*60*60 - time.zone.should == nil - end + time.utc_offset.should == -9*60*60 + time.zone.should == nil + end - it "returns a Time with UTC offset specified as a single letter military timezone" do - Time.new(2000, 1, 1, 0, 0, 0, in: "W").utc_offset.should == 3600 * -10 - end + it "returns a Time with UTC offset specified as a single letter military timezone" do + Time.new(2000, 1, 1, 0, 0, 0, in: "W").utc_offset.should == 3600 * -10 + end - it "could be a timezone object" do - zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo") - time = Time.new(2000, 1, 1, 12, 0, 0, in: zone) + it "could be a timezone object" do + zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo") + time = Time.new(2000, 1, 1, 12, 0, 0, in: zone) - time.utc_offset.should == 5*3600+30*60 - time.zone.should == zone + time.utc_offset.should == 5*3600+30*60 + time.zone.should == zone - zone = TimeSpecs::TimezoneWithName.new(name: "PST") - time = Time.new(2000, 1, 1, 12, 0, 0, in: zone) + zone = TimeSpecs::TimezoneWithName.new(name: "PST") + time = Time.new(2000, 1, 1, 12, 0, 0, in: zone) - time.utc_offset.should == -9*60*60 - time.zone.should == zone - end + time.utc_offset.should == -9*60*60 + time.zone.should == zone + end - it "allows omitting minor arguments" do - Time.new(2000, 1, 1, 12, 1, 1, in: "+05:00").should == Time.new(2000, 1, 1, 12, 1, 1, "+05:00") - Time.new(2000, 1, 1, 12, 1, in: "+05:00").should == Time.new(2000, 1, 1, 12, 1, 0, "+05:00") - Time.new(2000, 1, 1, 12, in: "+05:00").should == Time.new(2000, 1, 1, 12, 0, 0, "+05:00") - Time.new(2000, 1, 1, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00") - Time.new(2000, 1, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00") - Time.new(2000, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00") - Time.new(in: "+05:00").should be_close(Time.now.getlocal("+05:00"), TIME_TOLERANCE) - end + it "allows omitting minor arguments" do + Time.new(2000, 1, 1, 12, 1, 1, in: "+05:00").should == Time.new(2000, 1, 1, 12, 1, 1, "+05:00") + Time.new(2000, 1, 1, 12, 1, in: "+05:00").should == Time.new(2000, 1, 1, 12, 1, 0, "+05:00") + Time.new(2000, 1, 1, 12, in: "+05:00").should == Time.new(2000, 1, 1, 12, 0, 0, "+05:00") + Time.new(2000, 1, 1, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00") + Time.new(2000, 1, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00") + Time.new(2000, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00") + Time.new(in: "+05:00").should be_close(Time.now.getlocal("+05:00"), TIME_TOLERANCE) + end - it "converts to a provided timezone if all the positional arguments are omitted" do - Time.new(in: "+05:00").utc_offset.should == 5*3600 - end + it "converts to a provided timezone if all the positional arguments are omitted" do + Time.new(in: "+05:00").utc_offset.should == 5*3600 + end - it "raises ArgumentError if format is invalid" do - -> { Time.new(2000, 1, 1, 12, 0, 0, in: "+09:99") }.should raise_error(ArgumentError) - -> { Time.new(2000, 1, 1, 12, 0, 0, in: "ABC") }.should raise_error(ArgumentError) - end + it "raises ArgumentError if format is invalid" do + -> { Time.new(2000, 1, 1, 12, 0, 0, in: "+09:99") }.should.raise(ArgumentError) + -> { Time.new(2000, 1, 1, 12, 0, 0, in: "ABC") }.should.raise(ArgumentError) + end - it "raises ArgumentError if two offset arguments are given" do - -> { - Time.new(2000, 1, 1, 12, 0, 0, "+05:00", in: "+05:00") - }.should raise_error(ArgumentError, "timezone argument given as positional and keyword arguments") - end + it "raises ArgumentError if two offset arguments are given" do + -> { + Time.new(2000, 1, 1, 12, 0, 0, "+05:00", in: "+05:00") + }.should.raise(ArgumentError, "timezone argument given as positional and keyword arguments") end end - ruby_version_is "3.2" do - describe "Time.new with a String argument" do - it "parses an ISO-8601 like format" do - t = Time.utc(2020, 12, 24, 15, 56, 17) - - Time.new("2020-12-24T15:56:17Z").should == t - Time.new("2020-12-25 00:56:17 +09:00").should == t - Time.new("2020-12-25 00:57:47 +09:01:30").should == t - Time.new("2020-12-25 00:56:17 +0900").should == t - Time.new("2020-12-25 00:57:47 +090130").should == t - Time.new("2020-12-25T00:56:17+09:00").should == t + describe "Time.new with a String argument" do + it "parses an ISO-8601 like format" do + t = Time.utc(2020, 12, 24, 15, 56, 17) - Time.new("2020-12-25T00:56:17.123456+09:00").should == Time.utc(2020, 12, 24, 15, 56, 17, 123456) - end + Time.new("2020-12-24T15:56:17Z").should == t + Time.new("2020-12-25 00:56:17 +09:00").should == t + Time.new("2020-12-25 00:57:47 +09:01:30").should == t + Time.new("2020-12-25 00:56:17 +0900").should == t + Time.new("2020-12-25 00:57:47 +090130").should == t + Time.new("2020-12-25T00:56:17+09:00").should == t - it "accepts precision keyword argument and truncates specified digits of sub-second part" do - Time.new("2021-12-25 00:00:00.123456789876 +09:00").subsec.should == 0.123456789r - Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: nil).subsec.should == 0.123456789876r - Time.new("2021-12-25 00:00:00 +09:00", precision: 0).subsec.should == 0 - Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: -1).subsec.should == 0.123456789876r - end + Time.new("2020-12-25T00:56:17.123456+09:00").should == Time.utc(2020, 12, 24, 15, 56, 17, 123456) + end - it "returns Time in local timezone if not provided in the String argument" do - Time.new("2021-12-25 00:00:00").zone.should == Time.new(2021, 12, 25).zone - Time.new("2021-12-25 00:00:00").utc_offset.should == Time.new(2021, 12, 25).utc_offset - end + it "accepts precision keyword argument and truncates specified digits of sub-second part" do + Time.new("2021-12-25 00:00:00.123456789876 +09:00").subsec.should == 0.123456789r + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: nil).subsec.should == 0.123456789876r + Time.new("2021-12-25 00:00:00 +09:00", precision: 0).subsec.should == 0 + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: -1).subsec.should == 0.123456789876r + end - it "returns Time in timezone specified in the String argument" do - Time.new("2021-12-25 00:00:00 +05:00").to_s.should == "2021-12-25 00:00:00 +0500" - end + it "returns Time in local timezone if not provided in the String argument" do + Time.new("2021-12-25 00:00:00").zone.should == Time.new(2021, 12, 25).zone + Time.new("2021-12-25 00:00:00").utc_offset.should == Time.new(2021, 12, 25).utc_offset + end - it "returns Time in timezone specified in the String argument even if the in keyword argument provided" do - Time.new("2021-12-25 00:00:00 +09:00", in: "-01:00").to_s.should == "2021-12-25 00:00:00 +0900" - end + it "returns Time in timezone specified in the String argument" do + Time.new("2021-12-25 00:00:00 +05:00").to_s.should == "2021-12-25 00:00:00 +0500" + end - it "returns Time in timezone specified with in keyword argument if timezone isn't provided in the String argument" do - Time.new("2021-12-25 00:00:00", in: "-01:00").to_s.should == "2021-12-25 00:00:00 -0100" - end + it "returns Time in timezone specified in the String argument even if the in keyword argument provided" do + Time.new("2021-12-25 00:00:00 +09:00", in: "-01:00").to_s.should == "2021-12-25 00:00:00 +0900" + end - it "returns Time of Jan 1 for string with just year" do - Time.new("2021").should == Time.new(2021, 1, 1) - Time.new("2021").zone.should == Time.new(2021, 1, 1).zone - Time.new("2021").utc_offset.should == Time.new(2021, 1, 1).utc_offset - end + it "returns Time in timezone specified with in keyword argument if timezone isn't provided in the String argument" do + Time.new("2021-12-25 00:00:00", in: "-01:00").to_s.should == "2021-12-25 00:00:00 -0100" + end - it "returns Time of Jan 1 for string with just year in timezone specified with in keyword argument" do - Time.new("2021", in: "+17:00").to_s.should == "2021-01-01 00:00:00 +1700" - end + it "returns Time of Jan 1 for string with just year" do + Time.new("2021").should == Time.new(2021, 1, 1) + Time.new("2021").zone.should == Time.new(2021, 1, 1).zone + Time.new("2021").utc_offset.should == Time.new(2021, 1, 1).utc_offset + end - it "converts precision keyword argument into Integer if is not nil" do - obj = Object.new - def obj.to_int; 3; end + it "returns Time of Jan 1 for string with just year in timezone specified with in keyword argument" do + Time.new("2021", in: "+17:00").to_s.should == "2021-01-01 00:00:00 +1700" + end - Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: 1.2).subsec.should == 0.1r - Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: obj).subsec.should == 0.123r - Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: 3r).subsec.should == 0.123r - end + it "converts precision keyword argument into Integer if is not nil" do + obj = Object.new + def obj.to_int; 3; end - ruby_version_is ""..."3.3" do - it "raise TypeError is can't convert precision keyword argument into Integer" do - -> { - Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: "") - }.should raise_error(TypeError, "no implicit conversion from string") - end - end + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: 1.2).subsec.should == 0.1r + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: obj).subsec.should == 0.123r + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: 3r).subsec.should == 0.123r + end - ruby_version_is "3.3" do - it "raise TypeError is can't convert precision keyword argument into Integer" do - -> { - Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: "") - }.should raise_error(TypeError, "no implicit conversion of String into Integer") - end - end + it "returns Time with correct subseconds when given seconds fraction is shorted than 6 digits" do + Time.new("2020-12-25T00:56:17.123 +09:00").nsec.should == 123000000 + Time.new("2020-12-25T00:56:17.123 +09:00").usec.should == 123000 + Time.new("2020-12-25T00:56:17.123 +09:00").subsec.should == 0.123 + end - it "raises ArgumentError if part of time string is missing" do - -> { - Time.new("2020-12-25 00:56 +09:00") - }.should raise_error(ArgumentError, /missing sec part: 00:56 |can't parse:/) + it "returns Time with correct subseconds when given seconds fraction is milliseconds" do + Time.new("2020-12-25T00:56:17.123456 +09:00").nsec.should == 123456000 + Time.new("2020-12-25T00:56:17.123456 +09:00").usec.should == 123456 + Time.new("2020-12-25T00:56:17.123456 +09:00").subsec.should == 0.123456 + end - -> { - Time.new("2020-12-25 00 +09:00") - }.should raise_error(ArgumentError, /missing min part: 00 |can't parse:/) - end + it "returns Time with correct subseconds when given seconds fraction is longer that 6 digits but shorted than 9 digits" do + Time.new("2020-12-25T00:56:17.12345678 +09:00").nsec.should == 123456780 + Time.new("2020-12-25T00:56:17.12345678 +09:00").usec.should == 123456 + Time.new("2020-12-25T00:56:17.12345678 +09:00").subsec.should == 0.12345678 + end - ruby_version_is "3.2.3" do - it "raises ArgumentError if the time part is missing" do - -> { - Time.new("2020-12-25") - }.should raise_error(ArgumentError, /no time information|can't parse:/) - end - - it "raises ArgumentError if day is missing" do - -> { - Time.new("2020-12") - }.should raise_error(ArgumentError, /no time information|can't parse:/) - end - end + it "returns Time with correct subseconds when given seconds fraction is nanoseconds" do + Time.new("2020-12-25T00:56:17.123456789 +09:00").nsec.should == 123456789 + Time.new("2020-12-25T00:56:17.123456789 +09:00").usec.should == 123456 + Time.new("2020-12-25T00:56:17.123456789 +09:00").subsec.should == 0.123456789 + end - it "raises ArgumentError if subsecond is missing after dot" do - -> { - Time.new("2020-12-25 00:56:17. +0900") - }.should raise_error(ArgumentError, /subsecond expected after dot: 00:56:17. |can't parse:/) - end + it "returns Time with correct subseconds when given seconds fraction is longer than 9 digits" do + Time.new("2020-12-25T00:56:17.123456789876 +09:00").nsec.should == 123456789 + Time.new("2020-12-25T00:56:17.123456789876 +09:00").usec.should == 123456 + Time.new("2020-12-25T00:56:17.123456789876 +09:00").subsec.should == 0.123456789 + end - it "raises ArgumentError if String argument is not in the supported format" do - -> { - Time.new("021-12-25 00:00:00.123456 +09:00") - }.should raise_error(ArgumentError, /year must be 4 or more digits: 021|can't parse:/) + it "raise TypeError is can't convert precision keyword argument into Integer" do + -> { + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: "") + }.should.raise(TypeError, "no implicit conversion of String into Integer") + end - -> { - Time.new("2020-012-25 00:56:17 +0900") - }.should raise_error(ArgumentError, /\Atwo digits mon is expected after [`']-': -012-25 00:\z|can't parse:/) + it "raises ArgumentError if part of time string is missing" do + -> { + Time.new("2020-12-25 00:56 +09:00") + }.should.raise(ArgumentError, /missing sec part: 00:56 |can't parse:/) - -> { - Time.new("2020-2-25 00:56:17 +0900") - }.should raise_error(ArgumentError, /\Atwo digits mon is expected after [`']-': -2-25 00:56\z|can't parse:/) + -> { + Time.new("2020-12-25 00 +09:00") + }.should.raise(ArgumentError, /missing min part: 00 |can't parse:/) + end - -> { - Time.new("2020-12-215 00:56:17 +0900") - }.should raise_error(ArgumentError, /\Atwo digits mday is expected after [`']-': -215 00:56:\z|can't parse:/) + it "raises ArgumentError if the time part is missing" do + -> { + Time.new("2020-12-25") + }.should.raise(ArgumentError, /no time information|can't parse:/) + end - -> { - Time.new("2020-12-25 000:56:17 +0900") - }.should raise_error(ArgumentError, /two digits hour is expected: 000:56:17 |can't parse:/) + it "raises ArgumentError if day is missing" do + -> { + Time.new("2020-12") + }.should.raise(ArgumentError, /no time information|can't parse:/) + end - -> { - Time.new("2020-12-25 0:56:17 +0900") - }.should raise_error(ArgumentError, /two digits hour is expected: 0:56:17 \+0|can't parse:/) + it "raises ArgumentError if subsecond is missing after dot" do + -> { + Time.new("2020-12-25 00:56:17. +0900") + }.should.raise(ArgumentError, /subsecond expected after dot: 00:56:17. |can't parse:/) + end - -> { - Time.new("2020-12-25 00:516:17 +0900") - }.should raise_error(ArgumentError, /\Atwo digits min is expected after [`']:': :516:17 \+09\z|can't parse:/) + it "raises ArgumentError if String argument is not in the supported format" do + -> { + Time.new("021-12-25 00:00:00.123456 +09:00") + }.should.raise(ArgumentError, /year must be 4 or more digits: 021|can't parse:/) - -> { - Time.new("2020-12-25 00:6:17 +0900") - }.should raise_error(ArgumentError, /\Atwo digits min is expected after [`']:': :6:17 \+0900\z|can't parse:/) + -> { + Time.new("2020-012-25 00:56:17 +0900") + }.should.raise(ArgumentError, /\Atwo digits mon is expected after [`']-': -012-25 00:\z|can't parse:/) - -> { - Time.new("2020-12-25 00:56:137 +0900") - }.should raise_error(ArgumentError, /\Atwo digits sec is expected after [`']:': :137 \+0900\z|can't parse:/) + -> { + Time.new("2020-2-25 00:56:17 +0900") + }.should.raise(ArgumentError, /\Atwo digits mon is expected after [`']-': -2-25 00:56\z|can't parse:/) - -> { - Time.new("2020-12-25 00:56:7 +0900") - }.should raise_error(ArgumentError, /\Atwo digits sec is expected after [`']:': :7 \+0900\z|can't parse:/) + -> { + Time.new("2020-12-215 00:56:17 +0900") + }.should.raise(ArgumentError, /\Atwo digits mday is expected after [`']-': -215 00:56:\z|can't parse:/) - -> { - Time.new("2020-12-25 00:56. +0900") - }.should raise_error(ArgumentError, /fraction min is not supported: 00:56\.|can't parse:/) + -> { + Time.new("2020-12-25 000:56:17 +0900") + }.should.raise(ArgumentError, /two digits hour is expected: 000:56:17 |can't parse:/) - -> { - Time.new("2020-12-25 00. +0900") - }.should raise_error(ArgumentError, /fraction hour is not supported: 00\.|can't parse:/) - end + -> { + Time.new("2020-12-25 0:56:17 +0900") + }.should.raise(ArgumentError, /two digits hour is expected: 0:56:17 \+0|can't parse:/) - it "raises ArgumentError if date/time parts values are not valid" do - -> { - Time.new("2020-13-25 00:56:17 +09:00") - }.should raise_error(ArgumentError, /(mon|argument) out of range/) + -> { + Time.new("2020-12-25 00:516:17 +0900") + }.should.raise(ArgumentError, /\Atwo digits min is expected after [`']:': :516:17 \+09\z|can't parse:/) - -> { - Time.new("2020-12-32 00:56:17 +09:00") - }.should raise_error(ArgumentError, /(mday|argument) out of range/) + -> { + Time.new("2020-12-25 00:6:17 +0900") + }.should.raise(ArgumentError, /\Atwo digits min is expected after [`']:': :6:17 \+0900\z|can't parse:/) - -> { - Time.new("2020-12-25 25:56:17 +09:00") - }.should raise_error(ArgumentError, /(hour|argument) out of range/) + -> { + Time.new("2020-12-25 00:56:137 +0900") + }.should.raise(ArgumentError, /\Atwo digits sec is expected after [`']:': :137 \+0900\z|can't parse:/) - -> { - Time.new("2020-12-25 00:61:17 +09:00") - }.should raise_error(ArgumentError, /(min|argument) out of range/) + -> { + Time.new("2020-12-25 00:56:7 +0900") + }.should.raise(ArgumentError, /\Atwo digits sec is expected after [`']:': :7 \+0900\z|can't parse:/) - -> { - Time.new("2020-12-25 00:56:61 +09:00") - }.should raise_error(ArgumentError, /(sec|argument) out of range/) + -> { + Time.new("2020-12-25 00:56. +0900") + }.should.raise(ArgumentError, /fraction min is not supported: 00:56\.|can't parse:/) - -> { - Time.new("2020-12-25 00:56:17 +23:59:60") - }.should raise_error(ArgumentError, /utc_offset|argument out of range/) + -> { + Time.new("2020-12-25 00. +0900") + }.should.raise(ArgumentError, /fraction hour is not supported: 00\.|can't parse:/) + end - -> { - Time.new("2020-12-25 00:56:17 +24:00") - }.should raise_error(ArgumentError, /(utc_offset|argument) out of range/) + it "raises ArgumentError if date/time parts values are not valid" do + -> { + Time.new("2020-13-25 00:56:17 +09:00") + }.should.raise(ArgumentError, /(mon|argument) out of range/) - -> { - Time.new("2020-12-25 00:56:17 +23:61") - }.should raise_error(ArgumentError, /utc_offset/) - - ruby_bug '#20797', ''...'3.4' do - -> { - Time.new("2020-12-25 00:56:17 +00:23:61") - }.should raise_error(ArgumentError, /utc_offset/) - end - end + -> { + Time.new("2020-12-32 00:56:17 +09:00") + }.should.raise(ArgumentError, /(mday|argument) out of range/) - it "raises ArgumentError if utc offset parts are not valid" do - -> { Time.new("2020-12-25 00:56:17 +24:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.new("2020-12-25 00:56:17 +2400") }.should raise_error(ArgumentError, "utc_offset out of range") + -> { + Time.new("2020-12-25 25:56:17 +09:00") + }.should.raise(ArgumentError, /(hour|argument) out of range/) - -> { Time.new("2020-12-25 00:56:17 +99:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.new("2020-12-25 00:56:17 +9900") }.should raise_error(ArgumentError, "utc_offset out of range") + -> { + Time.new("2020-12-25 00:61:17 +09:00") + }.should.raise(ArgumentError, /(min|argument) out of range/) - -> { Time.new("2020-12-25 00:56:17 +00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') - -> { Time.new("2020-12-25 00:56:17 +0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') + -> { + Time.new("2020-12-25 00:56:61 +09:00") + }.should.raise(ArgumentError, /(sec|argument) out of range/) - -> { Time.new("2020-12-25 00:56:17 +00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') - -> { Time.new("2020-12-25 00:56:17 +0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') + -> { + Time.new("2020-12-25 00:56:17 +23:59:60") + }.should.raise(ArgumentError, /utc_offset|argument out of range/) - ruby_bug '#20797', ''...'3.4' do - -> { Time.new("2020-12-25 00:56:17 +00:00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') - -> { Time.new("2020-12-25 00:56:17 +000060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') + -> { + Time.new("2020-12-25 00:56:17 +24:00") + }.should.raise(ArgumentError, /(utc_offset|argument) out of range/) - -> { Time.new("2020-12-25 00:56:17 +00:00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') - -> { Time.new("2020-12-25 00:56:17 +000099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') - end - end + -> { + Time.new("2020-12-25 00:56:17 +23:61") + }.should.raise(ArgumentError, /utc_offset/) - it "raises ArgumentError if string has not ascii-compatible encoding" do + ruby_bug '#20797', ''...'3.4' do -> { - Time.new("2021-11-31 00:00:60 +09:00".encode("utf-32le")) - }.should raise_error(ArgumentError, "time string should have ASCII compatible encoding") + Time.new("2020-12-25 00:56:17 +00:23:61") + }.should.raise(ArgumentError, /utc_offset/) end + end - it "raises ArgumentError if string doesn't start with year" do - -> { - Time.new("a\nb") - }.should raise_error(ArgumentError, "can't parse: \"a\\nb\"") - end + it "raises ArgumentError if utc offset parts are not valid" do + -> { Time.new("2020-12-25 00:56:17 +24:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.new("2020-12-25 00:56:17 +2400") }.should.raise(ArgumentError, "utc_offset out of range") - it "raises ArgumentError if string has extra characters after offset" do - -> { - Time.new("2021-11-31 00:00:59 +09:00 abc") - }.should raise_error(ArgumentError, /can't parse.+ abc/) - end + -> { Time.new("2020-12-25 00:56:17 +99:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.new("2020-12-25 00:56:17 +9900") }.should.raise(ArgumentError, "utc_offset out of range") + + -> { Time.new("2020-12-25 00:56:17 +00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') + -> { Time.new("2020-12-25 00:56:17 +0060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') + + -> { Time.new("2020-12-25 00:56:17 +00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') + -> { Time.new("2020-12-25 00:56:17 +0099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') + + ruby_bug '#20797', ''...'3.4' do + -> { Time.new("2020-12-25 00:56:17 +00:00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') + -> { Time.new("2020-12-25 00:56:17 +000060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') - ruby_version_is "3.2.3" do - it "raises ArgumentError when there are leading space characters" do - -> { Time.new(" 2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("\t2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("\n2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("\v2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("\f2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("\r2020-12-02 00:00:00") }.should raise_error(ArgumentError, /can't parse/) - end - - it "raises ArgumentError when there are trailing whitespaces" do - -> { Time.new("2020-12-02 00:00:00 ") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("2020-12-02 00:00:00\t") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("2020-12-02 00:00:00\n") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("2020-12-02 00:00:00\v") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("2020-12-02 00:00:00\f") }.should raise_error(ArgumentError, /can't parse/) - -> { Time.new("2020-12-02 00:00:00\r") }.should raise_error(ArgumentError, /can't parse/) - end + -> { Time.new("2020-12-25 00:56:17 +00:00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') + -> { Time.new("2020-12-25 00:56:17 +000099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') end end + + it "raises ArgumentError if string has not ascii-compatible encoding" do + -> { + Time.new("2021-11-31 00:00:60 +09:00".encode("utf-32le")) + }.should.raise(ArgumentError, "time string should have ASCII compatible encoding") + end + + it "raises ArgumentError if string doesn't start with year" do + -> { + Time.new("a\nb") + }.should.raise(ArgumentError, "can't parse: \"a\\nb\"") + end + + it "raises ArgumentError if string has extra characters after offset" do + -> { + Time.new("2021-11-31 00:00:59 +09:00 abc") + }.should.raise(ArgumentError, /can't parse.+ abc/) + end + + it "raises ArgumentError when there are leading space characters" do + -> { Time.new(" 2020-12-02 00:00:00") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("\t2020-12-02 00:00:00") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("\n2020-12-02 00:00:00") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("\v2020-12-02 00:00:00") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("\f2020-12-02 00:00:00") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("\r2020-12-02 00:00:00") }.should.raise(ArgumentError, /can't parse/) + end + + it "raises ArgumentError when there are trailing whitespaces" do + -> { Time.new("2020-12-02 00:00:00 ") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("2020-12-02 00:00:00\t") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("2020-12-02 00:00:00\n") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("2020-12-02 00:00:00\v") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("2020-12-02 00:00:00\f") }.should.raise(ArgumentError, /can't parse/) + -> { Time.new("2020-12-02 00:00:00\r") }.should.raise(ArgumentError, /can't parse/) + end end end diff --git a/spec/ruby/core/time/now_spec.rb b/spec/ruby/core/time/now_spec.rb index 7c2425411a..533cf68380 100644 --- a/spec/ruby/core/time/now_spec.rb +++ b/spec/ruby/core/time/now_spec.rb @@ -4,84 +4,177 @@ require_relative 'shared/now' describe "Time.now" do it_behaves_like :time_now, :now - ruby_version_is '3.1' do # https://bugs.ruby-lang.org/issues/17485 - describe ":in keyword argument" do - it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do - time = Time.now(in: "+05:00") + describe ":in keyword argument" do + it "could be UTC offset as a String in '+HH:MM or '-HH:MM' format" do + time = Time.now(in: "+05:00") - time.utc_offset.should == 5*60*60 - time.zone.should == nil + time.utc_offset.should == 5*60*60 + time.zone.should == nil - time = Time.now(in: "-09:00") + time = Time.now(in: "-09:00") - time.utc_offset.should == -9*60*60 - time.zone.should == nil + time.utc_offset.should == -9*60*60 + time.zone.should == nil - time = Time.now(in: "-09:00:01") + time = Time.now(in: "-09:00:01") - time.utc_offset.should == -(9*60*60 + 1) - time.zone.should == nil - end + time.utc_offset.should == -(9*60*60 + 1) + time.zone.should == nil + end + + it "could be UTC offset as a number of seconds" do + time = Time.now(in: 5*60*60) + + time.utc_offset.should == 5*60*60 + time.zone.should == nil + + time = Time.now(in: -9*60*60) + + time.utc_offset.should == -9*60*60 + time.zone.should == nil + end + + it "returns a Time with UTC offset specified as a single letter military timezone" do + Time.now(in: "W").utc_offset.should == 3600 * -10 + end + + it "could be a timezone object" do + zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo") + time = Time.now(in: zone) - it "could be UTC offset as a number of seconds" do - time = Time.now(in: 5*60*60) + time.utc_offset.should == 5*3600+30*60 + time.zone.should == zone - time.utc_offset.should == 5*60*60 - time.zone.should == nil + zone = TimeSpecs::TimezoneWithName.new(name: "PST") + time = Time.now(in: zone) - time = Time.now(in: -9*60*60) + time.utc_offset.should == -9*60*60 + time.zone.should == zone + end + + it "raises ArgumentError if format is invalid" do + -> { Time.now(in: "+09:99") }.should.raise(ArgumentError) + -> { Time.now(in: "ABC") }.should.raise(ArgumentError) + end + + it "raises ArgumentError if String argument and hours greater than 23" do + -> { Time.now(in: "+24:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.now(in: "+2400") }.should.raise(ArgumentError, "utc_offset out of range") + + -> { Time.now(in: "+99:00") }.should.raise(ArgumentError, "utc_offset out of range") + -> { Time.now(in: "+9900") }.should.raise(ArgumentError, "utc_offset out of range") + end + + it "raises ArgumentError if String argument and minutes greater than 59" do + -> { Time.now(in: "+00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') + -> { Time.now(in: "+0060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') + + -> { Time.now(in: "+00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') + -> { Time.now(in: "+0099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') + end - time.utc_offset.should == -9*60*60 - time.zone.should == nil + ruby_bug '#20797', ''...'3.4' do + it "raises ArgumentError if String argument and seconds greater than 59" do + -> { Time.now(in: "+00:00:60") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') + -> { Time.now(in: "+000060") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') + + -> { Time.now(in: "+00:00:99") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') + -> { Time.now(in: "+000099") }.should.raise(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') end + end + end - it "returns a Time with UTC offset specified as a single letter military timezone" do - Time.now(in: "W").utc_offset.should == 3600 * -10 + describe "Timezone object" do # https://bugs.ruby-lang.org/issues/17485 + it "raises TypeError if timezone does not implement #utc_to_local method" do + zone = Object.new + def zone.local_to_utc(time) + time end - it "could be a timezone object" do - zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo") - time = Time.now(in: zone) + -> { + Time.now(in: zone) + }.should.raise(TypeError, /can't convert Object into an exact number/) + end - time.utc_offset.should == 5*3600+30*60 - time.zone.should == zone + it "does not raise exception if timezone does not implement #local_to_utc method" do + zone = Object.new + def zone.utc_to_local(time) + time + end - zone = TimeSpecs::TimezoneWithName.new(name: "PST") - time = Time.now(in: zone) + Time.now(in: zone).should.is_a?(Time) + end - time.utc_offset.should == -9*60*60 - time.zone.should == zone - end + # The result also should be a Time or Time-like object (not necessary to be the same class) + # or Integer. The zone of the result is just ignored. + describe "returned value by #utc_to_local and #local_to_utc methods" do + it "could be Time instance" do + zone = Object.new + def zone.utc_to_local(t) + time = Time.new(t.year, t.mon, t.day, t.hour, t.min, t.sec, t.utc_offset) + time + 60 * 60 # + 1 hour + end - it "raises ArgumentError if format is invalid" do - -> { Time.now(in: "+09:99") }.should raise_error(ArgumentError) - -> { Time.now(in: "ABC") }.should raise_error(ArgumentError) + Time.now(in: zone).should.is_a?(Time) + Time.now(in: zone).utc_offset.should == 3600 end - it "raises ArgumentError if String argument and hours greater than 23" do - -> { Time.now(in: "+24:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now(in: "+2400") }.should raise_error(ArgumentError, "utc_offset out of range") + it "could be Time subclass instance" do + zone = Object.new + def zone.utc_to_local(t) + 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: "+99:00") }.should raise_error(ArgumentError, "utc_offset out of range") - -> { Time.now(in: "+9900") }.should raise_error(ArgumentError, "utc_offset out of range") + Time.now(in: zone).should.is_a?(Time) + Time.now(in: zone).utc_offset.should == 3600 end - it "raises ArgumentError if String argument and minutes greater than 59" do - -> { Time.now(in: "+00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:60') - -> { Time.now(in: "+0060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0060') + it "could be Integer" do + zone = Object.new + def zone.utc_to_local(time) + time.to_i + 60*60 + end - -> { Time.now(in: "+00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:99') - -> { Time.now(in: "+0099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +0099') + Time.now(in: zone).should.is_a?(Time) + Time.now(in: zone).utc_offset.should == 60*60 end - ruby_bug '#20797', ''...'3.4' do - it "raises ArgumentError if String argument and seconds greater than 59" do - -> { Time.now(in: "+00:00:60") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:60') - -> { Time.now(in: "+000060") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000060') + it "could have any #zone and #utc_offset because they are ignored" do + zone = Object.new + def zone.utc_to_local(t) + Struct.new(:year, :mon, :mday, :hour, :min, :sec, :isdst, :to_i, :zone, :utc_offset) # rubocop:disable Lint/StructNewOverride + .new(t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.isdst, t.to_i, 'America/New_York', -5*60*60) + end + Time.now(in: zone).utc_offset.should == 0 + + zone = Object.new + def zone.utc_to_local(t) + Struct.new(:year, :mon, :mday, :hour, :min, :sec, :isdst, :to_i, :zone, :utc_offset) # rubocop:disable Lint/StructNewOverride + .new(t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.isdst, t.to_i, 'Asia/Tokyo', 9*60*60) + end + Time.now(in: zone).utc_offset.should == 0 - -> { Time.now(in: "+00:00:99") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +00:00:99') - -> { Time.now(in: "+000099") }.should raise_error(ArgumentError, '"+HH:MM", "-HH:MM", "UTC" or "A".."I","K".."Z" expected for utc_offset: +000099') + zone = Object.new + def zone.utc_to_local(t) + Time.new(t.year, t.mon, t.mday, t.hour, t.min, t.sec, 9*60*60) end + Time.now(in: zone).utc_offset.should == 0 + end + + it "raises ArgumentError if difference between argument and result is too large" do + zone = Object.new + def zone.utc_to_local(t) + 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 + + -> { + Time.now(in: zone) + }.should.raise(ArgumentError, "utc_offset out of range") end end end diff --git a/spec/ruby/core/time/plus_spec.rb b/spec/ruby/core/time/plus_spec.rb index 642393b615..6bd01bcdf3 100644 --- a/spec/ruby/core/time/plus_spec.rb +++ b/spec/ruby/core/time/plus_spec.rb @@ -17,9 +17,9 @@ describe "Time#+" do end it "raises a TypeError if given argument is a coercible String" do - -> { Time.now + "1" }.should raise_error(TypeError) - -> { Time.now + "0.1" }.should raise_error(TypeError) - -> { Time.now + "1/3" }.should raise_error(TypeError) + -> { Time.now + "1" }.should.raise(TypeError) + -> { Time.now + "0.1" }.should.raise(TypeError) + -> { Time.now + "1/3" }.should.raise(TypeError) end it "increments the time by the specified amount as rational numbers" do @@ -32,8 +32,8 @@ describe "Time#+" do end it "raises TypeError on argument that can't be coerced into Rational" do - -> { Time.now + Object.new }.should raise_error(TypeError) - -> { Time.now + "stuff" }.should raise_error(TypeError) + -> { Time.now + Object.new }.should.raise(TypeError) + -> { Time.now + "stuff" }.should.raise(TypeError) end it "returns a UTC time if self is UTC" do @@ -68,15 +68,15 @@ describe "Time#+" do it "does not return a subclass instance" do c = Class.new(Time) x = c.now + 1 - x.should be_an_instance_of(Time) + x.should.instance_of?(Time) end it "raises TypeError on Time argument" do - -> { Time.now + Time.now }.should raise_error(TypeError) + -> { Time.now + Time.now }.should.raise(TypeError) end it "raises TypeError on nil argument" do - -> { Time.now + nil }.should raise_error(TypeError) + -> { Time.now + nil }.should.raise(TypeError) end #see [ruby-dev:38446] diff --git a/spec/ruby/core/time/round_spec.rb b/spec/ruby/core/time/round_spec.rb index 0cbed04ade..a739cabfdf 100644 --- a/spec/ruby/core/time/round_spec.rb +++ b/spec/ruby/core/time/round_spec.rb @@ -20,8 +20,8 @@ describe "Time#round" do it "returns an instance of Time, even if #round is called on a subclass" do subclass = Class.new(Time) instance = subclass.at(0) - instance.class.should equal subclass - instance.round.should be_an_instance_of(Time) + instance.class.should.equal? subclass + instance.round.should.instance_of?(Time) end it "copies own timezone to the returning value" do diff --git a/spec/ruby/core/time/shared/gmtime.rb b/spec/ruby/core/time/shared/gmtime.rb index 7b4f65f0b7..aa76b436cc 100644 --- a/spec/ruby/core/time/shared/gmtime.rb +++ b/spec/ruby/core/time/shared/gmtime.rb @@ -18,7 +18,7 @@ describe :time_gmtime, shared: true do it "returns self" do with_timezone("CST", -6) do t = Time.local(2007, 1, 9, 12, 0, 0) - t.send(@method).should equal(t) + t.send(@method).should.equal?(t) end end @@ -26,14 +26,14 @@ describe :time_gmtime, shared: true do it "does not raise an error if already in UTC" do time = Time.gm(2007, 1, 9, 12, 0, 0) time.freeze - time.send(@method).should equal(time) + time.send(@method).should.equal?(time) end it "raises a FrozenError if the time is not UTC" do with_timezone("CST", -6) do time = Time.now time.freeze - -> { time.send(@method) }.should raise_error(FrozenError) + -> { time.send(@method) }.should.raise(FrozenError) end end end diff --git a/spec/ruby/core/time/shared/inspect.rb b/spec/ruby/core/time/shared/inspect.rb index 4133671924..82f7f3c686 100644 --- a/spec/ruby/core/time/shared/inspect.rb +++ b/spec/ruby/core/time/shared/inspect.rb @@ -16,6 +16,6 @@ describe :inspect, shared: true do end it "returns a US-ASCII encoded string" do - Time.now.send(@method).encoding.should equal(Encoding::US_ASCII) + Time.now.send(@method).encoding.should.equal?(Encoding::US_ASCII) end end diff --git a/spec/ruby/core/time/shared/now.rb b/spec/ruby/core/time/shared/now.rb index f4018d72f4..839cfdcd2a 100644 --- a/spec/ruby/core/time/shared/now.rb +++ b/spec/ruby/core/time/shared/now.rb @@ -2,8 +2,8 @@ require_relative '../fixtures/classes' describe :time_now, shared: true do it "creates a subclass instance if called on a subclass" do - TimeSpecs::SubTime.send(@method).should be_an_instance_of(TimeSpecs::SubTime) - TimeSpecs::MethodHolder.send(@method).should be_an_instance_of(Time) + TimeSpecs::SubTime.send(@method).should.instance_of?(TimeSpecs::SubTime) + TimeSpecs::MethodHolder.send(@method).should.instance_of?(Time) end it "sets the current time" do diff --git a/spec/ruby/core/time/shared/time_params.rb b/spec/ruby/core/time/shared/time_params.rb index b6a6c88c8e..f0de986b8e 100644 --- a/spec/ruby/core/time/shared/time_params.rb +++ b/spec/ruby/core/time/shared/time_params.rb @@ -30,7 +30,7 @@ describe :time_params, shared: true do end it "raises a TypeError if the year is nil" do - -> { Time.send(@method, nil) }.should raise_error(TypeError) + -> { Time.send(@method, nil) }.should.raise(TypeError) end it "accepts nil month, day, hour, minute, and second" do @@ -148,48 +148,52 @@ describe :time_params, shared: true do # For some reason MRI uses a different message for month in 13-15 and month>=16 -> { Time.send(@method, 2008, 16, 31, 23, 59, 59) - }.should raise_error(ArgumentError, /(mon|argument) out of range/) + }.should.raise(ArgumentError, /(mon|argument) out of range/) end it "raises an ArgumentError for out of range day" do -> { Time.send(@method, 2008, 12, 32, 23, 59, 59) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an ArgumentError for out of range hour" do -> { Time.send(@method, 2008, 12, 31, 25, 59, 59) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an ArgumentError for out of range minute" do -> { Time.send(@method, 2008, 12, 31, 23, 61, 59) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end it "raises an ArgumentError for out of range second" do # For some reason MRI uses different messages for seconds 61-63 and seconds >= 64 -> { Time.send(@method, 2008, 12, 31, 23, 59, 61) - }.should raise_error(ArgumentError, /(sec|argument) out of range/) + }.should.raise(ArgumentError, /(sec|argument) out of range/) -> { Time.send(@method, 2008, 12, 31, 23, 59, -1) - }.should raise_error(ArgumentError, "argument out of range") + }.should.raise(ArgumentError, "argument out of range") + end + + it "raises ArgumentError when given 8 arguments" do + -> { Time.send(@method, *[0]*8) }.should.raise(ArgumentError) end it "raises ArgumentError when given 9 arguments" do - -> { Time.send(@method, *[0]*9) }.should raise_error(ArgumentError) + -> { Time.send(@method, *[0]*9) }.should.raise(ArgumentError) end it "raises ArgumentError when given 11 arguments" do - -> { Time.send(@method, *[0]*11) }.should raise_error(ArgumentError) + -> { Time.send(@method, *[0]*11) }.should.raise(ArgumentError) end it "returns subclass instances" do c = Class.new(Time) - c.send(@method, 2008, "12").should be_an_instance_of(c) + c.send(@method, 2008, "12").should.instance_of?(c) end end @@ -209,23 +213,23 @@ describe :time_params_10_arg, shared: true do it "raises an ArgumentError for out of range values" do -> { Time.send(@method, 61, 59, 23, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored) - }.should raise_error(ArgumentError) # sec + }.should.raise(ArgumentError) # sec -> { Time.send(@method, 59, 61, 23, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored) - }.should raise_error(ArgumentError) # min + }.should.raise(ArgumentError) # min -> { Time.send(@method, 59, 59, 25, 31, 12, 2008, :ignored, :ignored, :ignored, :ignored) - }.should raise_error(ArgumentError) # hour + }.should.raise(ArgumentError) # hour -> { Time.send(@method, 59, 59, 23, 32, 12, 2008, :ignored, :ignored, :ignored, :ignored) - }.should raise_error(ArgumentError) # day + }.should.raise(ArgumentError) # day -> { Time.send(@method, 59, 59, 23, 31, 13, 2008, :ignored, :ignored, :ignored, :ignored) - }.should raise_error(ArgumentError) # month + }.should.raise(ArgumentError) # month end end @@ -236,7 +240,7 @@ describe :time_params_microseconds, shared: true do end it "raises an ArgumentError for out of range microsecond" do - -> { Time.send(@method, 2000, 1, 1, 20, 15, 1, 1000000) }.should raise_error(ArgumentError) + -> { Time.send(@method, 2000, 1, 1, 20, 15, 1, 1000000) }.should.raise(ArgumentError) end it "handles fractional microseconds as a Float" do diff --git a/spec/ruby/core/time/strftime_spec.rb b/spec/ruby/core/time/strftime_spec.rb index 4cb300c916..1528a668a1 100644 --- a/spec/ruby/core/time/strftime_spec.rb +++ b/spec/ruby/core/time/strftime_spec.rb @@ -25,7 +25,7 @@ describe "Time#strftime" do # Differences with date it "requires an argument" do - -> { @time.strftime }.should raise_error(ArgumentError) + -> { @time.strftime }.should.raise(ArgumentError) end # %Z is zone name or empty for Time @@ -50,44 +50,42 @@ describe "Time#strftime" do time.strftime("%::z").should == "+01:01:05" end - ruby_version_is "3.1" do - it "supports RFC 3339 UTC for unknown offset local time, -0000, as %-z" do - time = Time.gm(2022) + it "supports RFC 3339 UTC for unknown offset local time, -0000, as %-z" do + time = Time.gm(2022) - time.strftime("%z").should == "+0000" - time.strftime("%-z").should == "-0000" - time.strftime("%-:z").should == "-00:00" - time.strftime("%-::z").should == "-00:00:00" - end + time.strftime("%z").should == "+0000" + time.strftime("%-z").should == "-0000" + time.strftime("%-:z").should == "-00:00" + time.strftime("%-::z").should == "-00:00:00" + end - it "applies '-' flag to UTC time" do - time = Time.utc(2022) - time.strftime("%-z").should == "-0000" + it "applies '-' flag to UTC time" do + time = Time.utc(2022) + time.strftime("%-z").should == "-0000" - time = Time.gm(2022) - time.strftime("%-z").should == "-0000" + time = Time.gm(2022) + time.strftime("%-z").should == "-0000" - time = Time.new(2022, 1, 1, 0, 0, 0, "Z") - time.strftime("%-z").should == "-0000" + time = Time.new(2022, 1, 1, 0, 0, 0, "Z") + time.strftime("%-z").should == "-0000" - time = Time.new(2022, 1, 1, 0, 0, 0, "-00:00") - time.strftime("%-z").should == "-0000" + time = Time.new(2022, 1, 1, 0, 0, 0, "-00:00") + time.strftime("%-z").should == "-0000" - time = Time.new(2022, 1, 1, 0, 0, 0, "+03:00").utc - time.strftime("%-z").should == "-0000" - end + time = Time.new(2022, 1, 1, 0, 0, 0, "+03:00").utc + time.strftime("%-z").should == "-0000" + end - it "ignores '-' flag for non-UTC time" do - time = Time.new(2022, 1, 1, 0, 0, 0, "+03:00") - time.strftime("%-z").should == "+0300" - end + it "ignores '-' flag for non-UTC time" do + time = Time.new(2022, 1, 1, 0, 0, 0, "+03:00") + time.strftime("%-z").should == "+0300" + end - it "works correctly with width, _ and 0 flags, and :" do - Time.now.utc.strftime("%-_10z").should == " -000" - Time.now.utc.strftime("%-10z").should == "-000000000" - Time.now.utc.strftime("%-010:z").should == "-000000:00" - Time.now.utc.strftime("%-_10:z").should == " -0:00" - Time.now.utc.strftime("%-_10::z").should == " -0:00:00" - end + it "works correctly with width, _ and 0 flags, and :" do + Time.now.utc.strftime("%-_10z").should == " -000" + Time.now.utc.strftime("%-10z").should == "-000000000" + Time.now.utc.strftime("%-010:z").should == "-000000:00" + Time.now.utc.strftime("%-_10:z").should == " -0:00" + Time.now.utc.strftime("%-_10::z").should == " -0:00:00" end end diff --git a/spec/ruby/core/time/subsec_spec.rb b/spec/ruby/core/time/subsec_spec.rb index 0f2c4eb856..3ed1bf5dd1 100644 --- a/spec/ruby/core/time/subsec_spec.rb +++ b/spec/ruby/core/time/subsec_spec.rb @@ -2,26 +2,26 @@ require_relative '../../spec_helper' describe "Time#subsec" do it "returns 0 as an Integer for a Time with a whole number of seconds" do - Time.at(100).subsec.should eql(0) + Time.at(100).subsec.should.eql?(0) end it "returns the fractional seconds as a Rational for a Time constructed with a Rational number of seconds" do - Time.at(Rational(3, 2)).subsec.should eql(Rational(1, 2)) + Time.at(Rational(3, 2)).subsec.should.eql?(Rational(1, 2)) end it "returns the fractional seconds as a Rational for a Time constructed with a Float number of seconds" do - Time.at(10.75).subsec.should eql(Rational(3, 4)) + Time.at(10.75).subsec.should.eql?(Rational(3, 4)) end it "returns the fractional seconds as a Rational for a Time constructed with an Integer number of microseconds" do - Time.at(0, 999999).subsec.should eql(Rational(999999, 1000000)) + Time.at(0, 999999).subsec.should.eql?(Rational(999999, 1000000)) end it "returns the fractional seconds as a Rational for a Time constructed with an Rational number of microseconds" do - Time.at(0, Rational(9, 10)).subsec.should eql(Rational(9, 10000000)) + Time.at(0, Rational(9, 10)).subsec.should.eql?(Rational(9, 10000000)) end it "returns the fractional seconds as a Rational for a Time constructed with an Float number of microseconds" do - Time.at(0, 0.75).subsec.should eql(Rational(3, 4000000)) + Time.at(0, 0.75).subsec.should.eql?(Rational(3, 4000000)) end end diff --git a/spec/ruby/core/time/to_r_spec.rb b/spec/ruby/core/time/to_r_spec.rb index 6af2d9b7ea..e30f5d8f94 100644 --- a/spec/ruby/core/time/to_r_spec.rb +++ b/spec/ruby/core/time/to_r_spec.rb @@ -2,10 +2,10 @@ require_relative '../../spec_helper' describe "Time#to_r" do it "returns the a Rational representing seconds and subseconds since the epoch" do - Time.at(Rational(11, 10)).to_r.should eql(Rational(11, 10)) + Time.at(Rational(11, 10)).to_r.should.eql?(Rational(11, 10)) end it "returns a Rational even for a whole number of seconds" do - Time.at(2).to_r.should eql(Rational(2)) + Time.at(2).to_r.should.eql?(Rational(2)) end end diff --git a/spec/ruby/core/time/utc_spec.rb b/spec/ruby/core/time/utc_spec.rb index 566509fd33..ab3c0df657 100644 --- a/spec/ruby/core/time/utc_spec.rb +++ b/spec/ruby/core/time/utc_spec.rb @@ -22,10 +22,8 @@ describe "Time#utc?" do Time.now.localtime("UTC").utc?.should == true Time.at(Time.now, in: 'UTC').utc?.should == true - ruby_version_is "3.1" do - Time.new(2022, 1, 1, 0, 0, 0, in: "UTC").utc?.should == true - Time.now(in: "UTC").utc?.should == true - end + Time.new(2022, 1, 1, 0, 0, 0, in: "UTC").utc?.should == true + Time.now(in: "UTC").utc?.should == true end it "does treat time with Z offset as UTC" do @@ -33,26 +31,26 @@ describe "Time#utc?" do Time.now.localtime("Z").utc?.should == true Time.at(Time.now, in: 'Z').utc?.should == true - ruby_version_is "3.1" do - Time.new(2022, 1, 1, 0, 0, 0, in: "Z").utc?.should == true - Time.now(in: "Z").utc?.should == true - end + Time.new(2022, 1, 1, 0, 0, 0, in: "Z").utc?.should == true + Time.now(in: "Z").utc?.should == true end - ruby_version_is "3.1" do - it "does treat time with -00:00 offset as UTC" do - Time.new(2022, 1, 1, 0, 0, 0, "-00:00").utc?.should == true - Time.now.localtime("-00:00").utc?.should == true - Time.at(Time.now, in: '-00:00').utc?.should == true - end + it "does treat time with -00:00 offset as UTC" do + Time.new(2022, 1, 1, 0, 0, 0, "-00:00").utc?.should == true + Time.now.localtime("-00:00").utc?.should == true + Time.at(Time.now, in: '-00:00').utc?.should == true end it "does not treat time with +00:00 offset as UTC" do Time.new(2022, 1, 1, 0, 0, 0, "+00:00").utc?.should == false + Time.now.localtime("+00:00").utc?.should == false + Time.at(Time.now, in: "+00:00").utc?.should == false end it "does not treat time with 0 offset as UTC" do Time.new(2022, 1, 1, 0, 0, 0, 0).utc?.should == false + Time.now.localtime(0).utc?.should == false + Time.at(Time.now, in: 0).utc?.should == false end end diff --git a/spec/ruby/core/time/zone_spec.rb b/spec/ruby/core/time/zone_spec.rb index 63c92602d1..2cb3c5e7bb 100644 --- a/spec/ruby/core/time/zone_spec.rb +++ b/spec/ruby/core/time/zone_spec.rb @@ -6,7 +6,7 @@ describe "Time#zone" do with_timezone("America/New_York") do Time.new(2001, 1, 1, 0, 0, 0).zone.should == "EST" Time.new(2001, 7, 1, 0, 0, 0).zone.should == "EDT" - %w[EST EDT].should include Time.now.zone + %w[EST EDT].should.include? Time.now.zone end end end @@ -29,7 +29,7 @@ describe "Time#zone" do t = Time.new(2005, 2, 27, 22, 50, 0, -3600) with_timezone("America/New_York") do - t.getlocal("+05:00").zone.should be_nil + t.getlocal("+05:00").zone.should == nil end end @@ -69,22 +69,18 @@ describe "Time#zone" do Time.at(Time.now, in: 'UTC').zone.should == "UTC" Time.at(Time.now, in: 'Z').zone.should == "UTC" - ruby_version_is "3.1" do - Time.new(2022, 1, 1, 0, 0, 0, "-00:00").zone.should == "UTC" - Time.now.localtime("-00:00").zone.should == "UTC" - Time.at(Time.now, in: '-00:00').zone.should == "UTC" - end + Time.new(2022, 1, 1, 0, 0, 0, "-00:00").zone.should == "UTC" + Time.now.localtime("-00:00").zone.should == "UTC" + Time.at(Time.now, in: '-00:00').zone.should == "UTC" - ruby_version_is "3.1" do - Time.new(2022, 1, 1, 0, 0, 0, in: "UTC").zone.should == "UTC" - Time.new(2022, 1, 1, 0, 0, 0, in: "Z").zone.should == "UTC" + Time.new(2022, 1, 1, 0, 0, 0, in: "UTC").zone.should == "UTC" + Time.new(2022, 1, 1, 0, 0, 0, in: "Z").zone.should == "UTC" - Time.now(in: 'UTC').zone.should == "UTC" - Time.now(in: 'Z').zone.should == "UTC" + Time.now(in: 'UTC').zone.should == "UTC" + Time.now(in: 'Z').zone.should == "UTC" - Time.at(Time.now, in: 'UTC').zone.should == "UTC" - Time.at(Time.now, in: 'Z').zone.should == "UTC" - end + Time.at(Time.now, in: 'UTC').zone.should == "UTC" + Time.at(Time.now, in: 'Z').zone.should == "UTC" end platform_is_not :aix, :windows do |
