From f46a274064be7de45eb71a6260961fbdeff8e348 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 21 May 2024 22:11:03 -0500 Subject: [PATCH 1/3] Defer loading the cache for constructing fixed time zones --- src/types/timezone.jl | 15 ++++++++++----- test/types/timezone.jl | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) 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/timezone.jl b/test/types/timezone.jl index 0ca6db54c..2525bc18a 100644 --- a/test/types/timezone.jl +++ b/test/types/timezone.jl @@ -25,3 +25,19 @@ 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) + @test TimeZone("UTC") + @test isempty(TimeZones._TZ_CACHE) + finally + TimeZones._reload_tz_cache(TimeZones._COMPILED_DIR[]) + end +end From 5173ca4b6bee7cb11d62e6526198f0484e9b8348 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 21 May 2024 22:56:49 -0500 Subject: [PATCH 2/3] fixup! Defer loading the cache for constructing fixed time zones --- test/types/timezone.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/types/timezone.jl b/test/types/timezone.jl index 2525bc18a..b2368d636 100644 --- a/test/types/timezone.jl +++ b/test/types/timezone.jl @@ -35,7 +35,8 @@ end try @test isempty(TimeZones._TZ_CACHE) - @test TimeZone("UTC") + + TimeZone("UTC") # Construct a `FixedTimeZone` without loading the tz cache @test isempty(TimeZones._TZ_CACHE) finally TimeZones._reload_tz_cache(TimeZones._COMPILED_DIR[]) From fb926a03c03d0b09bfe8828cfdf4734c1ee4100c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 21 May 2024 22:57:05 -0500 Subject: [PATCH 3/3] Fix FixedTimeZone constructor --- src/types/fixedtimezone.jl | 2 +- test/types/fixedtimezone.jl | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) 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/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")