Skip to content

Commit be1a184

Browse files
committed
Merge remote-tracking branch 'origin/main' into mp/project-datasets
2 parents c428180 + 8963f09 commit be1a184

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

src/JuliaHub.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import TOML
1313
import URIs
1414
using UUIDs: UUIDs, UUID
1515

16+
# We cache the local timezone in a global, so that we don't have to call
17+
# TimeZones.localzone() every time we do a TZ operation. However, we only
18+
# populate this when we actually call _localtz(). We used to do this in __init_,
19+
# but that caused a noticeable startup lag.
1620
const _LOCAL_TZ = Ref{Dates.TimeZone}()
1721

1822
include("utils.jl")
@@ -32,13 +36,6 @@ include("jobs/logging-kafka.jl")
3236
include("jobs/logging-legacy.jl")
3337
include("projects.jl")
3438

35-
function __init__()
36-
# We'll only attempt to determine the local timezone once, when the package loads,
37-
# and store the result in a global. This way all timestamps will have consistent timezones
38-
# even if something in the environment changes.
39-
_LOCAL_TZ[] = _localtz()
40-
end
41-
4239
# JuliaHub.jl follows the convention that all private names are
4340
# prefixed with an underscore.
4441
function _find_public_names()

src/authentication.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ function _authenticate(
335335
force::Bool, maxcount::Integer, hook::Union{Base.Callable, Nothing},
336336
project_id::Union{UUID, Nothing},
337337
)
338+
# So this is a bit weird, but we want to ensure that the global _LOCAL_TZ[] is initialized
339+
# in a somewhat reliable way. Generally, constructing the authentication object is the first
340+
# thing that you do in a session, so we just call _localtz() here, even though we don't
341+
# need it. This will ensure that the _LOCAL_TZ[] timezone object "cache" is populated
342+
# as soon as you start using JuliaHub.jl, but _not_ when you load it, due to the effect
343+
# that has on load time -- this function is pretty heavy, so the _localtz() call is not
344+
# significant anyway.
345+
_localtz()
346+
338347
isnothing(hook) || PkgAuthentication.register_open_browser_hook(hook)
339348
try
340349
# _authenticate either returns a valid token, or throws

src/utils.jl

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ end
397397
_utc2localtz(timestamp::Number) = _utc2localtz(Dates.unix2datetime(timestamp))
398398
function _utc2localtz(datetime_utc::Dates.DateTime)::TimeZones.ZonedDateTime
399399
datetimez_utc = TimeZones.ZonedDateTime(datetime_utc, TimeZones.tz"UTC")
400-
return TimeZones.astimezone(datetimez_utc, _LOCAL_TZ[])
400+
return TimeZones.astimezone(datetimez_utc, _localtz())
401401
end
402402
# Special version of _utc2localtz to handle integer ms timestamp
403403
function _ms_utc2localtz(timestamp::Integer)::TimeZones.ZonedDateTime
@@ -446,17 +446,26 @@ function _parse_tz(timestamp_str::AbstractString; msg::Union{AbstractString, Not
446446
end
447447
throw(JuliaHubError(errmsg))
448448
end
449-
return TimeZones.astimezone(timestamp, _LOCAL_TZ[])
449+
return TimeZones.astimezone(timestamp, _localtz())
450450
end
451451

452-
# It's quite easy to make TimeZones.localzone() fail and throw.
453-
# So this wraps it, and adds a UTC fallback (which seems like the sensible
454-
# default) in the case where somehow the local timezone is not configured properly.
455-
function _localtz()
456-
try
457-
TimeZones.localzone()
458-
catch e
459-
@debug "Unable to determine local timezone" exception = (e, catch_backtrace())
460-
TimeZones.tz"UTC"
452+
# This function is internally used where we need to pass the local timezone
453+
# for datetime printing or parsing functions.
454+
function _localtz()::Dates.TimeZone
455+
global _LOCAL_TZ
456+
if isassigned(_LOCAL_TZ)
457+
return _LOCAL_TZ[]
458+
else
459+
# It's quite easy to make TimeZones.localzone() fail and throw.
460+
# So this wraps it, and adds a UTC fallback (which seems like the sensible
461+
# default) in the case where somehow the local timezone is not configured properly.
462+
tz = try
463+
TimeZones.localzone()
464+
catch e
465+
@debug "Unable to determine local timezone" exception = (e, catch_backtrace())
466+
TimeZones.tz"UTC"
467+
end
468+
_LOCAL_TZ[] = tz
469+
return tz
461470
end
462471
end

test/utils.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,28 @@ end
5252
end
5353

5454
@testset "_parse_tz" begin
55+
@test JuliaHub._localtz() isa Dates.TimeZone
5556
@test isassigned(JuliaHub._LOCAL_TZ)
57+
@test JuliaHub._localtz() === JuliaHub._LOCAL_TZ[]
5658
let t = JuliaHub._parse_tz("2022-10-12T05:30:31.1+00:00")
5759
@test t isa TimeZones.ZonedDateTime
58-
@test t.timezone == JuliaHub._LOCAL_TZ[]
60+
@test t.timezone == JuliaHub._localtz()
5961
@test Dates.millisecond(t) == 100
6062
end
6163
let t = JuliaHub._parse_tz("2022-10-12T05:30:31.12+00:00")
6264
@test t isa TimeZones.ZonedDateTime
63-
@test t.timezone == JuliaHub._LOCAL_TZ[]
65+
@test t.timezone == JuliaHub._localtz()
6466
@test Dates.millisecond(t) == 120
6567
end
6668
let t = JuliaHub._parse_tz("2022-10-12T05:30:31.123+00:00")
6769
@test t isa TimeZones.ZonedDateTime
68-
@test t.timezone == JuliaHub._LOCAL_TZ[]
70+
@test t.timezone == JuliaHub._localtz()
6971
@test Dates.millisecond(t) == 123
7072
end
7173
@test_throws JuliaHub.JuliaHubError JuliaHub._parse_tz("2022-10-12T05:30:31.+00:00")
7274
let t = JuliaHub._parse_tz("2022-10-12T05:30:31+00:00")
7375
@test t isa TimeZones.ZonedDateTime
74-
@test t.timezone == JuliaHub._LOCAL_TZ[]
76+
@test t.timezone == JuliaHub._localtz()
7577
@test Dates.millisecond(t) == 0
7678
end
7779
@test_throws JuliaHub.JuliaHubError JuliaHub._parse_tz("")

0 commit comments

Comments
 (0)