Bug #11595


Time#utc? and Time#gmt? return misleading results based on $TZ

Added by davidcelis (David Celis) almost 7 years ago. Updated almost 6 years ago.

Target version:
ruby -v:
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]


There is an issue with Time#utc? and its alias, Time#gmt?, that return misleading results based on the value of the TZ environment variable. It seems that the only way for a Time instance to return true for utc? is if you explicitly call #utc on it before:

ENV['TZ'] = 'UTC'
# => "UTC"
time =
# => 2015-10-14 19:30:00 +0000
# => false
time = time.utc
# => 2015-10-14 19:30:00 UTC
# => true

This seems misleading based on the value of $TZ being "UTC". The expected result for calling in this case would be true, as would that be expected for time zones that are considered links to "UTC" based on the tzdata list. These include "UTC", "GMT", "Etc/UTC", "Etc/GMT", "Universal", etc.


time_utc.patch (927 Bytes) time_utc.patch nkmrya (Yasuhiro Nakamura), 11/30/2015 01:27 PM

Updated by nkmrya (Yasuhiro Nakamura) over 6 years ago

  • Assignee set to akr (Akira Tanaka)

Updated by nkmrya (Yasuhiro Nakamura) over 6 years ago

Anyway I write a patch.

Is it a bug? or specification?

Updated by davidcelis (David Celis) over 6 years ago

Yasuhiro Nakamura wrote:

Anyway I write a patch.

Is it a bug? or specification?

To me it seems like a bug, since the expectation is that Time#utc? would return true for any time with an offset of 0

Updated by avit (Andrew Vit) over 6 years ago

Careful: not every time with offset 0 is UTC.

A time zone with offset +0100/-0100 may have DST rules, and should not be utc?.

Updated by akr (Akira Tanaka) almost 6 years ago

  • Status changed from Open to Rejected

Current behavior is intentional.

Time#utc? returns the mode of Time object, not the time zone offset is zero.

The mode affects several methods.

For example, Time#to_s generates "UTC" for UTC Time objects and
numeric timezone offset string for non-UTC Time objects.
The result of Time#utc? method means the difference of them.

% TZ=GMT ruby -e '
u = Time.utc(2000)
l = Time.local(2000)
p [u, u.utc?]
p [l, l.utc?]'       
[2000-01-01 00:00:00 UTC, true]
[2000-01-01 00:00:00 +0000, false]

Also available in: Atom PDF