diff --git a/spec/std/time/location_spec.cr b/spec/std/time/location_spec.cr index bcf7595cffd8..78e27a230fde 100644 --- a/spec/std/time/location_spec.cr +++ b/spec/std/time/location_spec.cr @@ -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 diff --git a/src/time/location.cr b/src/time/location.cr index d84c1ab14218..7e0e8f160cb9 100644 --- a/src/time/location.cr +++ b/src/time/location.cr @@ -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 @@ -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 when "Local" local @@ -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 @@ -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