Repository navigation
Time.at(t, in: nil) returns UTC instead of the local time (or the time zone of t), unlike CRuby #10018
Description
Environment Information
- JRuby:
jruby 10.1.3.0 (4.0.9) 2026-11-06 ffffffffff OpenJDK 64-Bit Server VM 27.0.1.0.3 on 27.0.1.0.3 +indy +jit [arm64-darwin]
(also reproduced withjruby 10.1.2.0 (4.0.0) 2026-09-21) - Command line:
jruby script.rb, no flags,JRUBY_OPTSempty - OS:
Darwin 27.0.0 ... arm64(macOS, Apple silicon) - Environment:
TZ=Europe/Parisset on the command line (the system time zone is alsoEurope/Paris) - For comparison: CRuby 4.0.9 (
ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [arm64-darwin27])
Expected Behavior
in: defaults to nil in the documented signature of Time.at
(at(time, subsec = false, unit = :microsecond, in: nil)), so passing in: nil explicitly
should be the same as not passing in:: the result is in the local time zone, or keeps the
time zone of t when t is a Time. CRuby does this. A library that forwards an optional
time zone to Time.at(t, in: zone) works as is, with zone being nil when the caller
wants the default.
Actual Behavior
With only one positional argument, Time.at(t, in: nil) returns a UTC Time (utc? is true,
utc_offset is 0) even though the local time zone is not UTC.
Time.at(t), Time.at(t, 0, in: nil), Time.now(in: nil), Time.new(..., in: nil) and
Time.at(t).getlocal(nil) are correct on JRuby.
Reproduction:
# TZ=Europe/Paris jruby tz_nil.rb
p RUBY_DESCRIPTION
p Time.at(0, in: nil)
p Time.at(0, in: nil).utc?
p Time.at(0, in: nil).utc_offset
p Time.at(0)
p Time.at(0).utc?
p Time.at(0, 0, in: nil)
p Time.at(0).getlocal(nil)
p Time.now(in: nil).utc_offset
p Time.new(2025, 1, 1, in: nil).utc_offsetJRuby 10.1.2.0 and 10.1.3.0 (TZ=Europe/Paris):
1970-01-01 00:00:00 UTC # Time.at(0, in: nil) <-- wrong
true # .utc? <-- wrong
0 # .utc_offset <-- wrong
1970-01-01 01:00:00 +0100 # Time.at(0)
false
1970-01-01 01:00:00 +0100 # Time.at(0, 0, in: nil)
1970-01-01 01:00:00 +0100 # Time.at(0).getlocal(nil)
3600 # Time.now(in: nil).utc_offset
3600 # Time.new(2025, 1, 1, in: nil).utc_offset
CRuby 4.0.9 (TZ=Europe/Paris):
1970-01-01 01:00:00 +0100 # Time.at(0, in: nil)
false # .utc?
3600 # .utc_offset
1970-01-01 01:00:00 +0100 # Time.at(0)
false
1970-01-01 01:00:00 +0100 # Time.at(0, 0, in: nil)
1970-01-01 01:00:00 +0100 # Time.at(0).getlocal(nil)
3600
3600
The result is the same without TZ set (the system time zone is Europe/Paris): on JRuby 10.1.3.0,
Time.at(0, in: nil) still returns 1970-01-01 00:00:00 UTC, while CRuby 4.0.9 returns
1970-01-01 01:00:00 +0100.
It also happens when the first argument is a Time: Time.at(t, in: nil) should keep the time zone
of t (same as Time.at(t)), but JRuby returns UTC.
# TZ=Europe/Paris
l = Time.new(2025, 12, 31, 1, 0, 0)
o = Time.new(2025, 12, 31, 3, 0, 0, "+03:00")
p Time.at(l, in: nil) # JRuby: 2025-12-31 00:00:00 UTC CRuby: 2025-12-31 01:00:00 +0100
p Time.at(o, in: nil) # JRuby: 2025-12-31 00:00:00 UTC CRuby: 2025-12-31 03:00:00 +0300
p Time.at(l) # JRuby: 2025-12-31 01:00:00 +0100 (correct)If t is already UTC (Time.utc(...)), both give UTC, so there is no difference.
Impact: a library that stores an optional time zone and calls Time.at(t, in: @tz).iso8601
prints ...Z instead of the local offset when @tz is nil.
Workaround: Time.at(t).getlocal(tz). It gives the same result as Time.at(t, in: tz) on CRuby
for nil, "UTC", "Z", "+HH:MM" and integer offsets. On JRuby 10.1.3.0 the two forms
are identical for every value of tz except nil, where only getlocal returns local time:
# TZ=Europe/Paris, t = 1_767_139_200
Time.at(t, in: nil) # JRuby: 2025-12-31 00:00:00 UTC CRuby: 2025-12-31 01:00:00 +0100
Time.at(t).getlocal(nil) # JRuby: 2025-12-31 01:00:00 +0100 CRuby: 2025-12-31 01:00:00 +0100Possible fix
In RubyTime.atOpts (core/src/main/java/org/jruby/RubyTime.java), the one-argument path calls
time.gmtime(context) before checking in:, so a nil zone leaves the result in UTC.
atMulti (used by Time.at(t, 0, in: nil), which is correct) only converts to UTC when a zone is given.
The same can be done in atOpts:
--- a/core/src/main/java/org/jruby/RubyTime.java
+++ b/core/src/main/java/org/jruby/RubyTime.java
@@ -1563,10 +1563,8 @@
if (arg2.isNil() && (arg3 == null || arg3.isNil())) {
RubyTime time = at1(context, recv, arg1);
-
- time = time.gmtime(context);
- if (!zone.isNil()) time = timeZoneLocal(context, zone, time);
+ if (!zone.isNil()) time = timeZoneLocal(context, zone, time.gmtime(context));
return time;
}With this change Time.at(x, in: nil) takes the same path as Time.at(x), which already gives the CRuby
result on JRuby 10.1.3.0 for numbers and for Time arguments (local, UTC and fixed offset).
I read this on the current master and I have not built JRuby or run any test with this change.
I found no existing issue about this. #6229 (in: with a TZInfo::Timezone) and #9436
(utc? when the local zone is UTC) are different.
Activity
[-]`Time.at(t, in: nil)` returns UTC instead of local time, unlike CRuby (`Time.at(t, 0, in: nil)`, `Time.now(in: nil)` and `Time.new(..., in: nil)` are correct)[/-][+]`Time.at(t, in: nil)` returns UTC instead of the local time (or the time zone of `t`), unlike CRuby[/+]