|
378 | 378 | _utc2localtz(timestamp::Number) = _utc2localtz(Dates.unix2datetime(timestamp)) |
379 | 379 | function _utc2localtz(datetime_utc::Dates.DateTime)::TimeZones.ZonedDateTime |
380 | 380 | datetimez_utc = TimeZones.ZonedDateTime(datetime_utc, TimeZones.tz"UTC") |
381 | | - return TimeZones.astimezone(datetimez_utc, _LOCAL_TZ[]) |
| 381 | + return TimeZones.astimezone(datetimez_utc, _localtz()) |
382 | 382 | end |
383 | 383 | # Special version of _utc2localtz to handle integer ms timestamp |
384 | 384 | function _ms_utc2localtz(timestamp::Integer)::TimeZones.ZonedDateTime |
@@ -427,17 +427,26 @@ function _parse_tz(timestamp_str::AbstractString; msg::Union{AbstractString, Not |
427 | 427 | end |
428 | 428 | throw(JuliaHubError(errmsg)) |
429 | 429 | end |
430 | | - return TimeZones.astimezone(timestamp, _LOCAL_TZ[]) |
| 430 | + return TimeZones.astimezone(timestamp, _localtz()) |
431 | 431 | end |
432 | 432 |
|
433 | | -# It's quite easy to make TimeZones.localzone() fail and throw. |
434 | | -# So this wraps it, and adds a UTC fallback (which seems like the sensible |
435 | | -# default) in the case where somehow the local timezone is not configured properly. |
436 | | -function _localtz() |
437 | | - try |
438 | | - TimeZones.localzone() |
439 | | - catch e |
440 | | - @debug "Unable to determine local timezone" exception = (e, catch_backtrace()) |
441 | | - TimeZones.tz"UTC" |
| 433 | +# This function is internally used where we need to pass the local timezone |
| 434 | +# for datetime printing or parsing functions. |
| 435 | +function _localtz()::Dates.TimeZone |
| 436 | + global _LOCAL_TZ |
| 437 | + if isassigned(_LOCAL_TZ) |
| 438 | + return _LOCAL_TZ[] |
| 439 | + else |
| 440 | + # It's quite easy to make TimeZones.localzone() fail and throw. |
| 441 | + # So this wraps it, and adds a UTC fallback (which seems like the sensible |
| 442 | + # default) in the case where somehow the local timezone is not configured properly. |
| 443 | + tz = try |
| 444 | + TimeZones.localzone() |
| 445 | + catch e |
| 446 | + @debug "Unable to determine local timezone" exception = (e, catch_backtrace()) |
| 447 | + TimeZones.tz"UTC" |
| 448 | + end |
| 449 | + _LOCAL_TZ[] = tz |
| 450 | + return tz |
442 | 451 | end |
443 | 452 | end |
0 commit comments