Skip to content

Commit

Permalink
adds "Etc/UTC" for load_local
Browse files Browse the repository at this point in the history
  • Loading branch information
femto committed Jan 8, 2024
1 parent 86a02e0 commit fb7bdb6
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/time/location.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ require "./location/loader"
# Time::Location.local = Time::Location.load("America/New_York")
# ```
class Time::Location
# `InvalidLocationNameError` is raised if a location name cannot be found in
# the time zone database.
#
# See `Time::Location.load` for details.
# `InvalidLocationNameError` is raised if a location name cannot be found in
# the time zone database.
#
# See `Time::Location.load` for details.
class InvalidLocationNameError < Exception
getter name, source

def initialize(@name : String, @source : String? = nil)
msg = "Invalid location name: #{name}"
msg += " in #{source}" if source
msg += " in #{source}" if source
super msg
end
end
Expand All @@ -74,9 +74,9 @@ class Time::Location
# Some zones have a `name` or abbreviation (such as `PDT`, `CEST`).
# For an unnamed zone the formatted offset should be used as name.
struct Zone
# This is the `UTC` time zone with offset `+00:00`.
#
# It is the only zone offset used in `Time::Location::UTC`.
# This is the `UTC` time zone with offset `+00:00`.
#
# It is the only zone offset used in `Time::Location::UTC`.
UTC = new "UTC", 0, false

# Returns the offset from UTC in seconds.
Expand All @@ -94,10 +94,10 @@ class Time::Location
# Raises `InvalidTimezoneOffsetError` if *seconds* is outside the supported
# value range `-86_400..86_400` seconds (`-24:00` to `+24:00`).
def initialize(@name : String?, @offset : Int32, @dst : Bool)
# Maximum offsets of IANA time zone database are -12:00 and +14:00.
# +/-24 hours allows a generous padding for unexpected offsets.
# TODO: Maybe reduce to Int16 (+/- 18 hours).
raise InvalidTimezoneOffsetError.new(offset) if offset >= SECONDS_PER_DAY || offset <= -SECONDS_PER_DAY
# Maximum offsets of IANA time zone database are -12:00 and +14:00.
# +/-24 hours allows a generous padding for unexpected offsets.
# TODO: Maybe reduce to Int16 (+/- 18 hours).
raise InvalidTimezoneOffsetError.new(offset) if offset >= SECONDS_PER_DAY || offset <= -SECONDS_PER_DAY
end

# Returns the name of the zone.
Expand All @@ -111,7 +111,7 @@ class Time::Location
# `offset` in seconds and `"DST"` if `#dst?`, otherwise `"STD"`.
def inspect(io : IO) : Nil
io << "Time::Location::Zone("
io << @name << ' ' unless @name.nil?
io << @name << ' ' unless @name.nil?
format(io)
io << " (" << offset << "s)"
if dst?
Expand All @@ -131,15 +131,15 @@ class Time::Location
sign, hours, minutes, seconds = sign_hours_minutes_seconds

io << sign
io << '0' if hours < 10
io << '0' if hours < 10
io << hours
io << ':' if with_colon
io << '0' if minutes < 10
io << ':' if with_colon
io << '0' if minutes < 10
io << minutes

if with_seconds == true || (seconds != 0 && with_seconds == :auto)
io << ':' if with_colon
io << '0' if seconds < 10
io << ':' if with_colon
io << '0' if seconds < 10
io << seconds
end
end
Expand Down Expand Up @@ -185,7 +185,7 @@ class Time::Location
else
io << " DST"
end
io << " UTC" if utc?
io << " UTC" if utc?
io << ')'
end
end
Expand Down Expand Up @@ -282,8 +282,8 @@ class Time::Location
when "Local"
local
when .includes?(".."), .starts_with?('/'), .starts_with?('\\')
# No valid IANA Time Zone name contains a single dot,
# much less dot dot. Likewise, none begin with a slash.
# No valid IANA Time Zone name contains a single dot,
# much less dot dot. Likewise, none begin with a slash.
raise InvalidLocationNameError.new(name)
else
if zoneinfo = ENV["ZONEINFO"]?
Expand Down Expand Up @@ -348,7 +348,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 Expand Up @@ -424,7 +424,7 @@ class Time::Location
transition.when > unix_seconds
end || transitions.size

tx_index -= 1 unless tx_index == 0
tx_index -= 1 unless tx_index == 0
transition = transitions[tx_index]
range_end = transitions[tx_index + 1]?.try(&.when) || Int64::MAX

Expand Down Expand Up @@ -456,7 +456,7 @@ class Time::Location
while index > 0
index -= 1
zone = zones[index]
return zone unless zone.dst?
return zone unless zone.dst?
end
end

Expand All @@ -479,4 +479,4 @@ class Time::Location
def fixed? : Bool
zones.size <= 1
end
end
end

0 comments on commit fb7bdb6

Please sign in to comment.