Skip to content

Commit c387ffd

Browse files
committed
Support Time.new with :in keyword argument
1 parent 82f5ec3 commit c387ffd

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

spec/ruby/core/time/new_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ def zone.local_to_utc(t)
431431
time.zone.should == nil
432432
end
433433

434+
it "returns a Time with UTC offset specified as a single letter military timezone" do
435+
Time.new(2000, 1, 1, 0, 0, 0, in: "W").utc_offset.should == 3600 * -10
436+
end
437+
434438
it "could be a timezone object" do
435439
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
436440
time = Time.new(2000, 1, 1, 12, 0, 0, in: zone)
@@ -445,13 +449,29 @@ def zone.local_to_utc(t)
445449
time.zone.should == zone
446450
end
447451

452+
it "allows omitting minor arguments" do
453+
Time.new(2000, 1, 1, 12, 1, 1, in: "+05:00").should == Time.new(2000, 1, 1, 12, 1, 1, "+05:00")
454+
Time.new(2000, 1, 1, 12, 1, in: "+05:00").should == Time.new(2000, 1, 1, 12, 1, 0, "+05:00")
455+
Time.new(2000, 1, 1, 12, in: "+05:00").should == Time.new(2000, 1, 1, 12, 0, 0, "+05:00")
456+
Time.new(2000, 1, 1, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00")
457+
Time.new(2000, 1, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00")
458+
Time.new(2000, in: "+05:00").should == Time.new(2000, 1, 1, 0, 0, 0, "+05:00")
459+
Time.new(in: "+05:00").round(0).should == Time.now.getlocal("+05:00").round(0)
460+
end
461+
462+
it "converts to a provided timezone if all the positional arguments are omitted" do
463+
Time.new(in: "+05:00").utc_offset.should == 5*3600
464+
end
465+
448466
it "raises ArgumentError if format is invalid" do
449467
-> { Time.new(2000, 1, 1, 12, 0, 0, in: "+09:99") }.should raise_error(ArgumentError)
450468
-> { Time.new(2000, 1, 1, 12, 0, 0, in: "ABC") }.should raise_error(ArgumentError)
451469
end
452470

453471
it "raises ArgumentError if two offset arguments are given" do
454-
-> { Time.new(2000, 1, 1, 12, 0, 0, "+05:00", in: "+05:00") }.should raise_error(ArgumentError)
472+
-> {
473+
Time.new(2000, 1, 1, 12, 0, 0, "+05:00", in: "+05:00")
474+
}.should raise_error(ArgumentError, "timezone argument given as positional and keyword arguments")
455475
end
456476
end
457477
end

spec/tags/core/time/new_tags.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,4 @@ fails:Time.new with a timezone argument subject's class implements .find_timezon
2020
fails:Time.new with a timezone argument subject's class implements .find_timezone method calls .find_timezone to build a time object if passed zone name as a timezone argument
2121
fails:Time.new with a timezone argument subject's class implements .find_timezone method does not call .find_timezone if passed any not string/numeric/timezone timezone argument
2222
fails:Time.new has at least microsecond precision
23-
fails:Time.new with a timezone argument :in keyword argument could be UTC offset as a String in '+HH:MM or '-HH:MM' format
24-
fails:Time.new with a timezone argument :in keyword argument could be UTC offset as a number of seconds
2523
fails:Time.new with a timezone argument :in keyword argument could be a timezone object
26-
fails:Time.new with a timezone argument :in keyword argument raises ArgumentError if format is invalid

src/main/ruby/truffleruby/core/time.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,15 @@ def compose(offset, p1, p2=nil, p3=nil, p4=nil, p5=nil, p6=nil, p7=nil,
464464
end
465465
private :compose
466466

467-
def new(year=undefined, month=nil, day=nil, hour=nil, minute=nil, second=nil, utc_offset=nil)
467+
def new(year=undefined, month=nil, day=nil, hour=nil, minute=nil, second=nil, utc_offset=nil, **options)
468+
if utc_offset && options[:in]
469+
raise ArgumentError, 'timezone argument given as positional and keyword arguments'
470+
end
471+
472+
utc_offset ||= options[:in]
473+
468474
if Primitive.undefined?(year)
469-
self.now
475+
utc_offset ? self.now.getlocal(utc_offset) : self.now
470476
elsif Primitive.nil? utc_offset
471477
compose(:local, year, month, day, hour, minute, second)
472478
elsif utc_offset.instance_of?(String) && !utc_offset.encoding.ascii_compatible?

0 commit comments

Comments
 (0)