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

safestring(df) #7

Merged
merged 6 commits into from
Jul 26, 2022
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
1 change: 1 addition & 0 deletions experment/sweeps_to_note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coveralls is to slow right now
31 changes: 28 additions & 3 deletions src/dateformat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,33 @@ Dates.default_format(::Type{NanoDate}) = ISONanoDateFormat

NanoDate(nd::NanoDate, df::DateFormat) = format(nd, df)

# returns the specifier part as a string
Base.String(df::DateFormat) = string(df)[12:end-1]
# get the specifier part of a DateFormat as a string
# restricts the specifier to 9 subsecond digits
function safestring(df::DateFormat)
str1 = string(df)[12:end-1]
str2 = ""
sepidx = findlast('.', str1)
isnothing(sepidx) && return str1

if endswith(str1, "Z")
str1 = str1[1:end-1]
str2 = "Z"
elseif endswith(str1, "m") # "±hh:mm" or "±hhmm"
pmidx = findlast('h', str1)
if isnothing(pmidx) || pmidx == 1 || (str1[pmidx-1] != '+' || str1[pmidx-1] != '-')
return str1
else
pmidx -= 1
str1 = str1[1:pmidx-1]
str2 = str1[pmidx:end]
end
end

sidxlast = findlast('s', str1)
sidxmax = sepidx + 9
sidxlast = min(sidxlast, sidxmax)
str1[begin:sidxlast] * str2
end

omit(needle::Nothing, haystack::Nothing) = nothing
omit(needle, haystack::Nothing) = nothing
Expand Down Expand Up @@ -51,7 +76,7 @@ function findeach(needle, haystack)
end

function nanodateformat(nd::NanoDate, df::DateFormat; subsecsep::Union{Char,AbstractString}='.')
dfstr = String(df)
dfstr = safestring(df)
scount, sindices = findeach('s', dfstr)
idxsubsecsep = subsecsep == "" ? nothing : findlast(subsecsep, dfstr)
if !isnothing(idxsubsecsep)
Expand Down
10 changes: 5 additions & 5 deletions src/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ charvec2string(chrs::AbstractVector{Char}) = foldl(*, chrs; init = "")
function nanodate_format(nd, df)
nooffset(df)
indices = indexperiods(df)
dfstr = String(df)
dfstr = safestring(df)
chrs = string2charvec(dfstr)
indices = NamedTuple{(:yr, :mn, :dy, :hr, :mi, :sc, :ss)}(indices) # omit offset field
syms = keys(indices)
Expand Down Expand Up @@ -142,7 +142,7 @@ end
subsec_str = lpad(subsec_value, 9, '0')
supersec = datetime - Millisecond(datetime)
dfstr = String(df)
dfstr = safestring(df)
str = Dates.format(datetime, df)
value(nd.nanosecs) == 0 && return str
Expand Down Expand Up @@ -195,15 +195,15 @@ function nanodate_format(nd, df, sep)
str
end

nooffset(df::DateFormat) = nooffset(String(df))
nooffset(df::DateFormat) = nooffset(safestring(df))
function nooffset(str::AbstractString)
if occursin('+', str)
throw(ArgumentError("utc offsets are not supported in format(), use timestamp()"))
end
end


separate_offset(df::DateFormat) = separate_offset(String(df))
separate_offset(df::DateFormat) = separate_offset(safestring(df))

function separate_offset(str::AbstractString)
if isempty(str) || isdigit(str[end])
Expand Down Expand Up @@ -323,7 +323,7 @@ function getpart(r::UnitRange, str)
end
getpart(x::Nothing, str) = "0"

indexperiods(df::DateFormat) = indexperiods(String(df))
indexperiods(df::DateFormat) = indexperiods(safestring(df))

function indexperiods(dfstr::String)
str = strip(dfstr)
Expand Down