Skip to content

Commit 97c368d

Browse files
committed
Refactor Truffle::TimeOperations.compose to move out handling of two method signatures
1 parent 449c612 commit 97c368d

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

spec/ruby/core/time/shared/time_params.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@
179179
}.should raise_error(ArgumentError, "argument out of range")
180180
end
181181

182+
it "raises ArgumentError when given 8 arguments" do
183+
-> { Time.send(@method, *[0]*8) }.should raise_error(ArgumentError)
184+
end
185+
182186
it "raises ArgumentError when given 9 arguments" do
183187
-> { Time.send(@method, *[0]*9) }.should raise_error(ArgumentError)
184188
end

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ def new(year = undefined, month = undefined, day = nil, hour = nil, minute = nil
421421
elsif Primitive.is_a?(year, String) && month_undefined
422422
Truffle::TimeOperations.new_from_string(self, year, **options)
423423
elsif Primitive.nil? utc_offset
424-
Truffle::TimeOperations.compose(self, :local, year, month, day, hour, minute, second)
424+
Truffle::TimeOperations.compose(self, :local, year, month, day, hour, minute, second, nil, nil)
425425
elsif utc_offset == :std
426-
Truffle::TimeOperations.compose(self, :local, second, minute, hour, day, month, year, nil, nil, false, nil)
426+
Truffle::TimeOperations.compose(self, :local, year, month, day, hour, minute, second, nil, false)
427427
elsif utc_offset == :dst
428-
Truffle::TimeOperations.compose(self, :local, second, minute, hour, day, month, year, nil, nil, true, nil)
428+
Truffle::TimeOperations.compose(self, :local, year, month, day, hour, minute, second, nil, true)
429429
else
430430
if utc_offset_in_utc?(utc_offset)
431431
utc_offset = :utc
@@ -435,7 +435,7 @@ def new(year = undefined, month = undefined, day = nil, hour = nil, minute = nil
435435
utc_offset = Truffle::TimeOperations.calculate_utc_offset_with_timezone_object(utc_offset, :local_to_utc, as_utc) ||
436436
Truffle::TimeOperations.coerce_to_utc_offset(utc_offset)
437437
end
438-
result = Truffle::TimeOperations.compose(self, utc_offset, year, month, day, hour, minute, second)
438+
result = Truffle::TimeOperations.compose(self, utc_offset, year, month, day, hour, minute, second, nil, nil)
439439
Truffle::TimeOperations.set_zone_if_object(result, zone)
440440
result
441441
end
@@ -464,12 +464,12 @@ def now(**options)
464464
end
465465

466466
def local(*args)
467-
Truffle::TimeOperations.compose(self, :local, *args)
467+
Truffle::TimeOperations.compose_dual_signature(self, :local, *args)
468468
end
469469
alias_method :mktime, :local
470470

471471
def gm(*args)
472-
Truffle::TimeOperations.compose(self, :utc, *args)
472+
Truffle::TimeOperations.compose_dual_signature(self, :utc, *args)
473473
end
474474
alias_method :utc, :gm
475475
end

src/main/ruby/truffleruby/core/truffle/time_operations.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ module TimeOperations
1717

1818
# Handles Time methods .utc, .gm. .local, .mktime dual signature:
1919
# - year, month, day, hour, min, sec, usec
20-
# - sec, min, hour, day, month, year
21-
def self.compose(time_class, utc_offset, p1, p2 = nil, p3 = nil, p4 = nil, p5 = nil, p6 = nil, p7 = nil,
22-
yday = undefined, is_dst = undefined, tz = undefined)
20+
# - sec, min, hour, day, month, year, dummy, dummy, dummy, dummy
21+
def self.compose_dual_signature(time_class, utc_offset, p1, p2 = nil, p3 = nil, p4 = nil, p5 = nil, p6 = nil, p7 = nil,
22+
yday = undefined, is_dst = undefined, tz = undefined)
2323
if Primitive.undefined?(tz)
2424
unless Primitive.undefined?(is_dst)
2525
raise ArgumentError, 'wrong number of arguments (9 for 1..8)'
2626
end
2727

28-
year, month, mday, hour, min, sec, usec, is_dst = p1, p2, p3, p4, p5, p6, p7, -1
28+
year, month, mday, hour, min, sec, usec, is_dst = p1, p2, p3, p4, p5, p6, p7, nil
2929
else
30-
year, month, mday, hour, min, sec, usec, is_dst = p6, p5, p4, p3, p2, p1, 0, is_dst ? 1 : 0
30+
year, month, mday, hour, min, sec, usec, is_dst = p6, p5, p4, p3, p2, p1, 0, is_dst
3131
end
3232

33+
compose(time_class, utc_offset, year, month, mday, hour, min, sec, usec, is_dst)
34+
end
35+
36+
def self.compose(time_class, utc_offset, year, month, mday, hour, min, sec, usec, is_dst)
3337
if Primitive.is_a?(month, String) or month.respond_to?(:to_str)
3438
month = StringValue(month)
3539
month = MonthValue[month.upcase] || month.to_i
@@ -59,14 +63,14 @@ def self.compose(time_class, utc_offset, p1, p2 = nil, p3 = nil, p4 = nil, p5 =
5963

6064
case utc_offset
6165
when :utc
62-
is_dst = -1
66+
is_dst = nil
6367
is_utc = true
6468
utc_offset = nil
6569
when :local
6670
is_utc = false
6771
utc_offset = nil
6872
else
69-
is_dst = -1
73+
is_dst = nil
7074
is_utc = false
7175
end
7276

@@ -88,8 +92,9 @@ def self.compose(time_class, utc_offset, p1, p2 = nil, p3 = nil, p4 = nil, p5 =
8892
end
8993

9094
nsec ||= 0
95+
dst_code = is_dst ? 1 : (Primitive.nil?(is_dst) ? -1 : 0)
9196

92-
Primitive.time_s_from_array(time_class, sec, min, hour, mday, month, year, nsec, is_dst, is_utc, utc_offset)
97+
Primitive.time_s_from_array(time_class, sec, min, hour, mday, month, year, nsec, dst_code, is_utc, utc_offset)
9398
end
9499

95100
# MRI: time_init_parse()
@@ -108,7 +113,7 @@ def self.new_from_string(time_class, str, **options)
108113
(?:\s* (?<offset>\S+))?
109114
)?\z/x =~ str
110115

111-
# convert seconds fraction to milliseconds
116+
# convert seconds fraction to microseconds
112117
usec = if subsec
113118
ndigits = subsec.length
114119

@@ -121,7 +126,8 @@ def self.new_from_string(time_class, str, **options)
121126
nil
122127
end
123128

124-
return self.compose(time_class, self.utc_offset_for_compose(offset || options[:in]), year, month, mday, hour, min, sec, usec)
129+
utc_offset = self.utc_offset_for_compose(offset || options[:in])
130+
return self.compose(time_class, utc_offset, year, month, mday, hour, min, sec, usec, nil)
125131
end
126132

127133
raise ArgumentError, "can't parse: #{str.inspect}"

0 commit comments

Comments
 (0)