|
397 | 397 | _utc2localtz(timestamp::Number) = _utc2localtz(Dates.unix2datetime(timestamp)) |
398 | 398 | function _utc2localtz(datetime_utc::Dates.DateTime)::TimeZones.ZonedDateTime |
399 | 399 | datetimez_utc = TimeZones.ZonedDateTime(datetime_utc, TimeZones.tz"UTC") |
400 | | - return TimeZones.astimezone(datetimez_utc, _LOCAL_TZ[]) |
| 400 | + return TimeZones.astimezone(datetimez_utc, _localtz()) |
401 | 401 | end |
402 | 402 | # Special version of _utc2localtz to handle integer ms timestamp |
403 | 403 | function _ms_utc2localtz(timestamp::Integer)::TimeZones.ZonedDateTime |
@@ -446,17 +446,26 @@ function _parse_tz(timestamp_str::AbstractString; msg::Union{AbstractString, Not |
446 | 446 | end |
447 | 447 | throw(JuliaHubError(errmsg)) |
448 | 448 | end |
449 | | - return TimeZones.astimezone(timestamp, _LOCAL_TZ[]) |
| 449 | + return TimeZones.astimezone(timestamp, _localtz()) |
450 | 450 | end |
451 | 451 |
|
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 |
461 | 470 | end |
462 | 471 | end |
0 commit comments