diff --git a/core/time/new_spec.rb b/core/time/new_spec.rb index 69ec7bee5..6dce5b73f 100644 --- a/core/time/new_spec.rb +++ b/core/time/new_spec.rb @@ -475,4 +475,94 @@ def zone.local_to_utc(t) end end end + + ruby_version_is "3.2" do + it "parses a time string" 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 + end + + it "accepts precission keyword argument" do + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: 3).subsec.should == 0.123r + Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: nil).subsec.should == 0.123456789876r + 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/) + + -> { + Time.new("2020-12-25 00 +09:00") + }.should raise_error(ArgumentError, /missing min part/) + end + + it "raises ArgumentError if zone argument is passed without in: keyword" do + -> { + Time.new(2021, 1, 1, "+09:99") + }.should raise_error(ArgumentError, /invalid value for Integer()/) + 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. ") + end + + it "raises ArgumentError if string argument is not valid" do + -> { + Time.new("021-12-25 00:00:00.123456 +09:00") + }.should raise_error(ArgumentError, /year must be 4 or more/) + + -> { + Time.new("2020-012-25 00:56:17 +0900") + }.should raise_error(ArgumentError, /two digits mon.*\b012\b/) + + -> { + Time.new("2020-2-25 00:56:17 +0900") + }.should raise_error(ArgumentError, /two digits mon.*\b2\b/) + + -> { + Time.new("2020-12-215 00:56:17 +0900") + }.should raise_error(ArgumentError, /two digits mday.*\b215\b/) + + -> { + Time.new("2020-12-25 000:56:17 +0900") + }.should raise_error(ArgumentError, /two digits hour.*\b000\b/) + + -> { + Time.new("2020-12-25 0:56:17 +0900") + }.should raise_error(ArgumentError, /two digits hour.*\b0\b/) + + -> { + Time.new("2020-12-25 00:516:17 +0900") + }.should raise_error(ArgumentError, /two digits min.*:516\b/) + + -> { + Time.new("2020-12-25 00:6:17 +0900") + }.should raise_error(ArgumentError, /two digits min.*:6\b/) + + -> { + Time.new("2020-12-25 00:56:137 +0900") + }.should raise_error(ArgumentError, /two digits sec.*:137\b/) + + -> { + Time.new("2020-12-25 00:56:7 +0900") + }.should raise_error(ArgumentError, /two digits sec.*:7\b/) + + -> { + Time.new("2020-12-25 00:56. +0900") + }.should raise_error(ArgumentError, /fraction min is.*56\./) + + -> { + Time.new("2020-12-25 00. +0900") + }.should raise_error(ArgumentError, /fraction hour is.*00\./) + end + end end