Skip to content

Commit 741667d

Browse files
committed
Implement Time.at in parameter
1 parent 5474e16 commit 741667d

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Compatibility:
5959
* Update random implementation layout to be more compatible (#2234).
6060
* Set `RbConfig::CONFIG['LIBPATHFLAG'/'RPATHFLAG']` like MRI to let `$LIBPATH` changes in `extconf.rb` work.
6161
* Access to path and mode via `rb_io_t` from C has been changed to improve compatibility for io-console.
62+
* Implemented the `Time.at` `in:` parameter.
6263

6364
Performance:
6465

spec/tags/core/time/at_tags.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
fails:Time.at passed Numeric roundtrips a Rational produced by #to_r
2-
fails:Time.at :in keyword argument could be UTC offset as a String in '+HH:MM or '-HH:MM' format
3-
fails:Time.at :in keyword argument could be UTC offset as a number of seconds
42
fails:Time.at :in keyword argument could be a timezone object
53
fails:Time.at passed non-Time, non-Numeric with an argument that responds to #to_r needs for the argument to respond to #to_int too

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,25 @@ def _dump(limit = nil)
300300
private :_dump
301301

302302
class << self
303-
def at(sec, usec=undefined, unit=undefined)
304-
if Primitive.undefined?(usec)
305-
if Primitive.object_kind_of?(sec, Time)
306-
copy = allocate
307-
copy.send(:initialize_copy, sec)
308-
return copy
309-
elsif Primitive.object_kind_of?(sec, Integer)
310-
return Primitive.time_at self, sec, 0
311-
elsif Primitive.object_kind_of?(sec, Float) and sec >= 0.0
312-
ns = (sec % 1.0 * 1e9).round
313-
return Primitive.time_at self, sec.to_i, ns
314-
end
303+
def at(sec, usec=undefined, unit=undefined, **kwargs)
304+
# **kwargs is used here because 'in' is a ruby keyword
305+
offset = kwargs[:in] ? Truffle::Type.coerce_to_utc_offset(kwargs[:in]) : nil
306+
307+
result = if Primitive.undefined?(usec)
308+
if Primitive.object_kind_of?(sec, Time)
309+
copy = allocate
310+
copy.send(:initialize_copy, sec)
311+
copy
312+
elsif Primitive.object_kind_of?(sec, Integer)
313+
Primitive.time_at self, sec, 0
314+
elsif Primitive.object_kind_of?(sec, Float) and sec >= 0.0
315+
ns = (sec % 1.0 * 1e9).round
316+
Primitive.time_at self, sec.to_i, ns
317+
end
318+
end
319+
if result
320+
result = Primitive.time_localtime(result, offset) if offset
321+
return result
315322
end
316323

317324
if Primitive.object_kind_of?(sec, Time) && Primitive.object_kind_of?(usec, Integer)
@@ -343,7 +350,9 @@ def at(sec, usec=undefined, unit=undefined)
343350
sec += nsec / 1_000_000_000
344351
nsec %= 1_000_000_000
345352

346-
Primitive.time_at self, sec, nsec
353+
time = Primitive.time_at self, sec, nsec
354+
time = Primitive.time_localtime(time, offset) if offset
355+
time
347356
end
348357

349358
def from_array(sec, min, hour, mday, month, year, nsec, is_dst, is_utc, utc_offset)

0 commit comments

Comments
 (0)