Skip to content

Commit 4d00c2c

Browse files
committed
Refactor TimeOperations.compose and simplify code a bit
1 parent 72b5d1a commit 4d00c2c

File tree

2 files changed

+18
-37
lines changed

2 files changed

+18
-37
lines changed

src/main/java/org/truffleruby/core/time/TimeNodes.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ public abstract static class TimeSFromArrayPrimitiveNode extends PrimitiveArrayA
511511
@Child private TruffleString.FromJavaStringNode fromJavaStringNode;
512512

513513
@Specialization(guards = "(isutc || !isRubyDynamicObject(utcoffset)) || isNil(utcoffset)")
514+
@TruffleBoundary
514515
protected RubyTime timeSFromArray(
515516
RubyClass timeClass,
516517
int sec,
@@ -524,13 +525,7 @@ protected RubyTime timeSFromArray(
524525
boolean isutc,
525526
Object utcoffset) {
526527
final RubyLanguage language = getLanguage();
527-
return buildTime(language, timeClass, sec, min, hour, mday, month, year, nsec, isdst, isutc, utcoffset);
528-
}
529528

530-
@TruffleBoundary
531-
private RubyTime buildTime(RubyLanguage language, RubyClass timeClass, int sec, int min, int hour, int mday,
532-
int month,
533-
int year, int nsec, int isdst, boolean isutc, Object utcoffset) {
534529
if (nsec < 0 || nsec > 999999999 ||
535530
sec < 0 || sec > 60 || // MRI accepts sec=60, whether it is a leap second or not
536531
min < 0 || min > 59 ||

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

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,40 @@ module TimeOperations
1515
'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
1616
}
1717

18+
# Handles Time methods .utc, .gm. .local, .mktime dual signature:
19+
# - year, month, day, hour, min, sec, usec
20+
# - sec, min, hour, day, month, year
1821
def self.compose(time_class, utc_offset, p1, p2 = nil, p3 = nil, p4 = nil, p5 = nil, p6 = nil, p7 = nil,
1922
yday = undefined, is_dst = undefined, tz = undefined)
2023
if Primitive.undefined?(tz)
2124
unless Primitive.undefined?(is_dst)
2225
raise ArgumentError, 'wrong number of arguments (9 for 1..8)'
2326
end
2427

25-
year = p1
26-
month = p2
27-
mday = p3
28-
hour = p4
29-
min = p5
30-
sec = p6
31-
usec = p7
32-
is_dst = -1
28+
year, month, mday, hour, min, sec, usec, is_dst = p1, p2, p3, p4, p5, p6, p7, -1
3329
else
34-
year = p6
35-
month = p5
36-
mday = p4
37-
hour = p3
38-
min = p2
39-
sec = p1
40-
usec = 0
41-
is_dst = is_dst ? 1 : 0
30+
year, month, mday, hour, min, sec, usec, is_dst = p6, p5, p4, p3, p2, p1, 0, is_dst ? 1 : 0
4231
end
4332

4433
if Primitive.is_a?(month, String) or month.respond_to?(:to_str)
4534
month = StringValue(month)
4635
month = MonthValue[month.upcase] || month.to_i
4736

4837
raise ArgumentError, 'month argument out of range' unless month
49-
else
50-
month = Truffle::Type.coerce_to(month || 1, Integer, :to_int)
5138
end
5239

53-
year = Primitive.is_a?(year, String) ? year.to_i : Truffle::Type.coerce_to(year, Integer, :to_int)
54-
mday = Primitive.is_a?(mday, String) ? mday.to_i : Truffle::Type.coerce_to(mday || 1, Integer, :to_int)
55-
hour = Primitive.is_a?(hour, String) ? hour.to_i : Truffle::Type.coerce_to(hour || 0, Integer, :to_int)
56-
min = Primitive.is_a?(min, String) ? min.to_i : Truffle::Type.coerce_to(min || 0, Integer, :to_int)
40+
year = year.to_i if Primitive.is_a?(year, String)
41+
mday = mday.to_i if Primitive.is_a?(mday, String)
42+
hour = hour.to_i if Primitive.is_a?(hour, String)
43+
min = min.to_i if Primitive.is_a?(min, String)
44+
45+
# Ensure all the user provided numeric values fit into the Java Integer type.
46+
# sec and nsec are handled separately.
47+
year = Primitive.rb_num2int(year )
48+
month = Primitive.rb_num2int(month || 1)
49+
mday = Primitive.rb_num2int(mday || 1)
50+
hour = Primitive.rb_num2int(hour || 0)
51+
min = Primitive.rb_num2int(min || 0)
5752

5853
nsec = nil
5954
if Primitive.is_a?(usec, String)
@@ -75,15 +70,6 @@ def self.compose(time_class, utc_offset, p1, p2 = nil, p3 = nil, p4 = nil, p5 =
7570
is_utc = false
7671
end
7772

78-
# Ensure all the user provided numeric values fit into the Java Integer type.
79-
# sec and nsec are handled separately.
80-
Primitive.rb_num2int(min)
81-
Primitive.rb_num2int(hour)
82-
Primitive.rb_num2int(mday)
83-
Primitive.rb_num2int(month)
84-
Primitive.rb_num2int(year)
85-
86-
# handle sec and nsec
8773
if Primitive.is_a?(sec, String)
8874
sec = sec.to_i
8975
elsif nsec

0 commit comments

Comments
 (0)