Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/types/fixedtimezone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ UTC+15:45:21
function FixedTimeZone(s::AbstractString)
s == "Z" && return UTC_ZERO

m = match(FIXED_TIME_ZONE_REGEX, s)
m = match(FIXED_TIME_ZONE_REGEX, String(s))
m === nothing && throw(ArgumentError("Unrecognized time zone: $s"))

coefficient = m[:sign] == "-" ? -1 : 1
Expand Down
15 changes: 10 additions & 5 deletions src/types/timezone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ US/Pacific (UTC-8/UTC-7)
TimeZone(::AbstractString, ::Class)

function TimeZone(str::AbstractString, mask::Class=Class(:DEFAULT))
tz, class = get(_get_tz_cache(), str) do
if occursin(FIXED_TIME_ZONE_REGEX, str)
FixedTimeZone(str), Class(:FIXED)
else
# Avoid performing the initial load of the tz cache when only loading a fixed time zone.
# Doing this avoids having dependents of TimeZones.jl incurring the initial cache load
# cost when using `@tz_str` or `TimeZone` (at the top-level). Specifically, for
# packages using `tz"UTC"` we can still support construction at parse time without
# adding to the load time of the package when monitoring `@time_imports`.
tz, class = if mask & Class(:FIXED) == Class(:FIXED) && occursin(FIXED_TIME_ZONE_REGEX, str)
FixedTimeZone(str), Class(:FIXED)
else
get(_get_tz_cache(), str) do
throw(ArgumentError("Unknown time zone \"$str\""))
end
end
Expand All @@ -130,7 +135,7 @@ Africa/Nairobi (UTC+3)
```
"""
macro tz_str(str)
TimeZone(str)
return TimeZone(str)
end

"""
Expand Down
2 changes: 2 additions & 0 deletions test/types/fixedtimezone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
@test FixedTimeZone("+00:30") == FixedTimeZone("UTC+00:30", 1800)
@test FixedTimeZone("-00:30") == FixedTimeZone("UTC-00:30", -1800)

@test FixedTimeZone(Test.GenericString("UTC")) == FixedTimeZone("UTC", 0)

@test_throws ArgumentError FixedTimeZone("1")
@test_throws ArgumentError FixedTimeZone("01")
@test_throws ArgumentError FixedTimeZone("123")
Expand Down
17 changes: 17 additions & 0 deletions test/types/timezone.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ end
@test TimeZone("Etc/GMT+12", Class(:LEGACY)) == FixedTimeZone("Etc/GMT+12", -12 * 3600)
@test TimeZone("Etc/GMT-14", Class(:LEGACY)) == FixedTimeZone("Etc/GMT-14", 14 * 3600)
end

@testset "fixed time zone cache skip" begin
# Reset the tz cache as if we had just loaded the package
lock(TimeZones._TZ_CACHE_LOCK) do
TimeZones._TZ_CACHE_INITIALIZED[] = false
empty!(TimeZones._TZ_CACHE)
end

try
@test isempty(TimeZones._TZ_CACHE)

TimeZone("UTC") # Construct a `FixedTimeZone` without loading the tz cache
@test isempty(TimeZones._TZ_CACHE)
finally
TimeZones._reload_tz_cache(TimeZones._COMPILED_DIR[])
end
end