Skip to content

Commit 422f5ba

Browse files
committed
Refactor Truffle::TimeOperations.compose to accept nanoseconds instead of microseconds
1 parent 97c368d commit 422f5ba

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ def self.compose_dual_signature(time_class, utc_offset, p1, p2 = nil, p3 = nil,
3030
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)
33+
nsec = nil
34+
if Primitive.is_a?(usec, String)
35+
nsec = usec.to_i * 1000
36+
elsif usec
37+
nsec = (usec * 1000).to_i
38+
end
39+
40+
compose(time_class, utc_offset, year, month, mday, hour, min, sec, nsec, is_dst)
3441
end
3542

36-
def self.compose(time_class, utc_offset, year, month, mday, hour, min, sec, usec, is_dst)
43+
def self.compose(time_class, utc_offset, year, month, mday, hour, min, sec, nsec, is_dst)
3744
if Primitive.is_a?(month, String) or month.respond_to?(:to_str)
3845
month = StringValue(month)
3946
month = MonthValue[month.upcase] || month.to_i
@@ -54,13 +61,6 @@ def self.compose(time_class, utc_offset, year, month, mday, hour, min, sec, usec
5461
hour = Primitive.rb_num2int(hour || 0)
5562
min = Primitive.rb_num2int(min || 0)
5663

57-
nsec = nil
58-
if Primitive.is_a?(usec, String)
59-
nsec = usec.to_i * 1000
60-
elsif usec
61-
nsec = (usec * 1000).to_i
62-
end
63-
6464
case utc_offset
6565
when :utc
6666
is_dst = nil
@@ -113,21 +113,21 @@ def self.new_from_string(time_class, str, **options)
113113
(?:\s* (?<offset>\S+))?
114114
)?\z/x =~ str
115115

116-
# convert seconds fraction to microseconds
117-
usec = if subsec
116+
# convert seconds fraction to nanoseconds
117+
nsec = if subsec
118118
ndigits = subsec.length
119119

120-
if ndigits <= 6
121-
subsec.to_i * 10.pow(6 - ndigits)
120+
if ndigits <= 9
121+
subsec.to_i * 10.pow(9 - ndigits)
122122
else
123-
subsec.to_r / 10.pow(ndigits - 6) # convert to Rational to not loose precision
123+
subsec.to_i / 10.pow(ndigits - 9)
124124
end
125125
else
126126
nil
127127
end
128128

129129
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)
130+
return self.compose(time_class, utc_offset, year, month, mday, hour, min, sec, nsec, nil)
131131
end
132132

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

0 commit comments

Comments
 (0)