diff --git a/src/TimeZones.jl b/src/TimeZones.jl index 4b704d84..30db41e4 100644 --- a/src/TimeZones.jl +++ b/src/TimeZones.jl @@ -54,15 +54,8 @@ function __init__() # TimeZones.jl is loaded by `deps/build.jl` as we have yet to compile the tzdata. isdir(_COMPILED_DIR[]) && _reload_cache(_COMPILED_DIR[]) - # Base extension needs to happen everytime the module is loaded (issue #24) - Dates.CONVERSION_SPECIFIERS['z'] = TimeZone - Dates.CONVERSION_SPECIFIERS['Z'] = TimeZone - Dates.CONVERSION_DEFAULTS[TimeZone] = "" - Dates.CONVERSION_TRANSLATIONS[ZonedDateTime] = ( - Year, Month, Day, Hour, Minute, Second, Millisecond, TimeZone, - ) - - global ISOZonedDateTimeFormat = DateFormat("yyyy-mm-ddTHH:MM:SS.ssszzz") + # Dates extension needs to happen everytime the module is loaded (issue #24) + init_dates_extension() end include("utils.jl") diff --git a/src/parse.jl b/src/parse.jl index 8d4393cd..2ef08fd0 100644 --- a/src/parse.jl +++ b/src/parse.jl @@ -1,6 +1,22 @@ using Dates: DateFormat, DatePart, min_width, max_width, tryparsenext_base10 using TimeZones.TZData: MIN_YEAR, MAX_YEAR +function init_dates_extension() + Dates.CONVERSION_SPECIFIERS['z'] = TimeZone + Dates.CONVERSION_SPECIFIERS['Z'] = TimeZone + Dates.CONVERSION_DEFAULTS[TimeZone] = "" + Dates.CONVERSION_TRANSLATIONS[ZonedDateTime] = ( + Year, Month, Day, Hour, Minute, Second, Millisecond, TimeZone, + ) +end + +const ISOZonedDateTimeFormat = let + init_dates_extension() + DateFormat("yyyy-mm-ddTHH:MM:SS.ssszzz") +end + +Dates.default_format(::Type{ZonedDateTime}) = ISOZonedDateTimeFormat + function tryparsenext_fixedtz(str, i, len, min_width::Int=1, max_width::Int=0) i == len && str[i] === 'Z' && return ("Z", i+1) @@ -81,8 +97,6 @@ function Dates.format(io::IO, d::DatePart{'Z'}, zdt, locale) write(io, string(zdt.zone)) # In most cases will be an abbreviation. end -# Note: ISOZonedDateTimeFormat is defined in the module __init__ which means that this -# function can not be called from within this module. TODO: Ignore linting for this line function ZonedDateTime(str::AbstractString, df::DateFormat=ISOZonedDateTimeFormat) try parse(ZonedDateTime, str, df) @@ -101,8 +115,6 @@ function ZonedDateTime(str::AbstractString, format::AbstractString; locale::Abst ZonedDateTime(str, DateFormat(format, locale)) end -Dates.default_format(::Type{ZonedDateTime}) = ISOZonedDateTimeFormat - """ _parsesub_tzabbr(str, [i, len]) -> Union{Tuple{AbstractString, Integer}, Exception}