Skip to content

Time.new(..., "UTC") and localtime("UTC") are not utc? when ENV["TZ"] is "UTC" #10065

Closed

Description

@taxor03

When the TZ environment variable is "UTC", a Time made with the "UTC" zone argument is not a UTC time: Time.new(2022, 1, 1, 0, 0, 0, "UTC").utc?, Time.now.localtime("UTC").utc?, and the same with in: "UTC" or getlocal("UTC"), return false, and inspect shows +0000 instead of UTC. With TZ unset or set to another zone they return true, and "Z" and "-00:00" give a UTC time under every TZ. In CRuby the "UTC" zone argument always gives a UTC time; only times made in the local zone, like Time.now, are not utc? when TZ is "UTC" (Time#utc?).

ENV["TZ"] = "UTC"

p Time.now.utc?
p Time.new(2022, 1, 1, 0, 0, 0, "UTC").utc?
p Time.new(2022, 1, 1, 0, 0, 0, "UTC")
p Time.now.localtime("UTC").utc?
p Time.at(0, in: "UTC")

CRuby 4.0.9 (ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [aarch64-linux]), and 3.4.12 prints the same:

false
true
2022-01-01 00:00:00 UTC
true
1970-01-01 00:00:00 UTC

JRuby 10.0.8.0 (jruby 10.0.8.0 (3.4.12) 2026-11-05 ef393d4e54 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]):

false
false
2022-01-01 00:00:00 +0000
false
1970-01-01 00:00:00 +0000

JRuby 10.1.3.0 (jruby 10.1.3.0 (4.0.9) 2026-11-05 3db52d02f9 OpenJDK 64-Bit Server VM 21.0.13+9-LTS on 21.0.13+9-LTS +indy +jit [aarch64-linux]) and a build of master (54dd973) print the same.

The wrong zone stays for the rest of the process: once Time.now has run with TZ set to "UTC", Time.new(2022, 1, 1, 0, 0, 0, "UTC").utc? is still false after TZ is changed to "Asia/Seoul".

getTimeZoneFromTZString caches the zone for a TZ value in the runtime's timezone cache under the TZ string (RubyTime.java#L189-L195), and maps "UTC" to Etc/UTC on purpose so that Time.now is not utc? (L220-L225). getTimeZoneFromUtcOffset, which handles the zone argument, first looks up arg.toString() in the same cache (L255-L257), so it returns Etc/UTC before it reaches its "UTC" check that returns DateTimeZone.UTC (L289). isUTC() requires DateTimeZone.UTC (L661-L662). Master has the same code (L254-L257, L653-L654). In CRuby, utc_offset_arg returns UTC_ZONE for "UTC" whatever TZ is (time.c#L2247-L2248).

ruby/spec's "does treat time with 'UTC' offset as UTC" (utc_spec.rb#L20-L27) checks these calls, but it fails only when the spec process runs with TZ=UTC. #9436 is a different case (times in the local zone reported as utc? when TZ is unset and the system zone is UTC).

Found while comparing JRuby with CRuby on ruby/spec-based checks.

Activity

  1. modified the milestone: JRuby 10.1.4.0 on Nov 17, 2026
  2. added this to the JRuby 10.0.9.0 milestone on Nov 17, 2026
  3. headius commented on Nov 17, 2026

    @headius
    Member

    Fixed by #10069. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions