From ec7d23d6281b847707695769be31f54fcd0e12b9 Mon Sep 17 00:00:00 2001 From: ohbarye Date: Sun, 9 Oct 2022 00:44:45 +0900 Subject: [PATCH 1/3] Add specs: `Time#new` accepts `in` keyword --- core/time/new_spec.rb | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/core/time/new_spec.rb b/core/time/new_spec.rb index 09b4d03a44..aabf28e712 100644 --- a/core/time/new_spec.rb +++ b/core/time/new_spec.rb @@ -332,4 +332,55 @@ def zone.local_to_utc(t) 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") + + time.utc_offset.should == 5*60*60 + time.zone.should == nil + + time = Time.new(2000, 1, 1, 12, 0, 0, in: "-09:00") + + time.utc_offset.should == -9*60*60 + 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) + + 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.utc_offset.should == -9*60*60 + time.zone.should == nil + 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) + + 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) + + time.utc_offset.should == -9*60*60 + time.zone.should == zone + 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 two offset arguments are given" do + -> { Time.new(2000, 1, 1, 12, 0, 0, "+05:00", in: "+05:00") }.should raise_error(ArgumentError) + end + end + end end From fe751cbfb7fca8310326104286f4ccf32b4ab2f1 Mon Sep 17 00:00:00 2001 From: ohbarye Date: Sun, 9 Oct 2022 22:15:14 +0900 Subject: [PATCH 2/3] Add specs: `Time.at` raises ArgumentError if `in` keyword format is invalid --- core/time/at_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/time/at_spec.rb b/core/time/at_spec.rb index 2cc46ab8c9..74b1962a95 100644 --- a/core/time/at_spec.rb +++ b/core/time/at_spec.rb @@ -266,5 +266,10 @@ time.zone.should == zone time.to_i.should == @epoch_time 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) + end end end From 6b1c28add4665aa520746664d5d70e457abba6bf Mon Sep 17 00:00:00 2001 From: ohbarye Date: Sun, 9 Oct 2022 22:22:05 +0900 Subject: [PATCH 3/3] Add specs: `Time.new` accepts `in` keyword --- core/time/now_spec.rb | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/core/time/now_spec.rb b/core/time/now_spec.rb index 7dc7951996..2b2e53a17c 100644 --- a/core/time/now_spec.rb +++ b/core/time/now_spec.rb @@ -3,4 +3,49 @@ describe "Time.now" do it_behaves_like :time_now, :now + + 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 = Time.now(in: "-09:00") + + time.utc_offset.should == -9*60*60 + 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 "could be a timezone object" do + zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo") + time = Time.now(in: zone) + + time.utc_offset.should == 5*3600+30*60 + time.zone.should == zone + + zone = TimeSpecs::TimezoneWithName.new(name: "PST") + time = Time.now(in: zone) + + 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_error(ArgumentError) + -> { Time.now(in: "ABC") }.should raise_error(ArgumentError) + end + end end