diff --git a/src/types/fixedtimezone.jl b/src/types/fixedtimezone.jl index c7dacecb4..215af9a0f 100644 --- a/src/types/fixedtimezone.jl +++ b/src/types/fixedtimezone.jl @@ -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 diff --git a/src/types/timezone.jl b/src/types/timezone.jl index bee3ce838..aa6ef3dda 100644 --- a/src/types/timezone.jl +++ b/src/types/timezone.jl @@ -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 @@ -130,7 +135,7 @@ Africa/Nairobi (UTC+3) ``` """ macro tz_str(str) - TimeZone(str) + return TimeZone(str) end """ diff --git a/test/types/fixedtimezone.jl b/test/types/fixedtimezone.jl index a7a93c9cd..07c0ef861 100644 --- a/test/types/fixedtimezone.jl +++ b/test/types/fixedtimezone.jl @@ -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") diff --git a/test/types/timezone.jl b/test/types/timezone.jl index 0ca6db54c..b2368d636 100644 --- a/test/types/timezone.jl +++ b/test/types/timezone.jl @@ -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