Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ISOZonedDateTimeFormat a const #456

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/TimeZones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 16 additions & 4 deletions src/parse.jl
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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}
Expand Down
Loading