Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Etc/UTC time zone identifier without tzdb #14185

Merged
merged 8 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions spec/std/time/location_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class Time::Location
with_zoneinfo do
Location.load("UTC").should eq Location::UTC
Location.load("").should eq Location::UTC

# Etc/UTC could be pointing to anything
Location.load("Etc/UTC").should_not eq Location::UTC
Location.load("Etc/UTC").should eq Location::UTC
end
end

Expand Down
12 changes: 8 additions & 4 deletions src/time/location.cr
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class Time::Location
#
# *name* is understood to be a location name in the IANA Time
# Zone database, such as `"America/New_York"`. As special cases,
# `"UTC"` and empty string (`""`) return `Location::UTC`, and
# `"UTC"`, `"Etc/UTC"` and empty string (`""`) return `Location::UTC`, and
# `"Local"` returns `Location.local`.
#
# The implementation uses a list of system-specific paths to look for a time
Expand Down Expand Up @@ -277,7 +277,11 @@ class Time::Location
# `Location`, unless the time zone database has been updated in between.
def self.load(name : String) : Location
case name
when "", "UTC"
when "", "UTC", "Etc/UTC"
# `UTC` is a special identifier, empty string represents a fallback mechanism.
# `Etc/UTC` is technically a tzdb identifier which could potentially point to anything.
# But we map it to `Location::UTC` directly for convenience which allows it to work
# without a copy of the database.
UTC
femto marked this conversation as resolved.
Show resolved Hide resolved
when "Local"
local
Expand Down Expand Up @@ -339,7 +343,7 @@ class Time::Location
# The environment variable `ENV["TZ"]` is consulted for finding the time zone
# to use.
#
# * `"UTC"` and empty string (`""`) return `Location::UTC`
# * `"UTC"`, `"Etc/UTC"` and empty string (`""`) return `Location::UTC`
# * Any other value (such as `"Europe/Berlin"`) is tried to be resolved using
# `Location.load`.
# * If `ENV["TZ"]` is not set, the system's local time zone data will be used
Expand All @@ -348,7 +352,7 @@ class Time::Location
# `Location::UTC` is returned.
def self.load_local : Location
case tz = ENV["TZ"]?
when "", "UTC"
when "", "UTC", "Etc/UTC"
return UTC
when Nil
if localtime = Crystal::System::Time.load_localtime
Expand Down