Skip to content

Commit

Permalink
Merge pull request #7 from JuliaTime/max9_subsec_digits
Browse files Browse the repository at this point in the history
safestring(df)
  • Loading branch information
JeffreySarnoff authored Jul 26, 2022
2 parents 37a03f9 + fc0c6df commit 9e8156f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
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

0 comments on commit 9e8156f

Please sign in to comment.