Skip to content

Commit 9e8156f

Browse files
Merge pull request #7 from JuliaTime/max9_subsec_digits
safestring(df)
2 parents 37a03f9 + fc0c6df commit 9e8156f

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

experment/sweeps_to_note.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coveralls is to slow right now

src/dateformat.jl

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,33 @@ Dates.default_format(::Type{NanoDate}) = ISONanoDateFormat
33

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

6-
# returns the specifier part as a string
7-
Base.String(df::DateFormat) = string(df)[12:end-1]
6+
# get the specifier part of a DateFormat as a string
7+
# restricts the specifier to 9 subsecond digits
8+
function safestring(df::DateFormat)
9+
str1 = string(df)[12:end-1]
10+
str2 = ""
11+
sepidx = findlast('.', str1)
12+
isnothing(sepidx) && return str1
13+
14+
if endswith(str1, "Z")
15+
str1 = str1[1:end-1]
16+
str2 = "Z"
17+
elseif endswith(str1, "m") # "±hh:mm" or "±hhmm"
18+
pmidx = findlast('h', str1)
19+
if isnothing(pmidx) || pmidx == 1 || (str1[pmidx-1] != '+' || str1[pmidx-1] != '-')
20+
return str1
21+
else
22+
pmidx -= 1
23+
str1 = str1[1:pmidx-1]
24+
str2 = str1[pmidx:end]
25+
end
26+
end
27+
28+
sidxlast = findlast('s', str1)
29+
sidxmax = sepidx + 9
30+
sidxlast = min(sidxlast, sidxmax)
31+
str1[begin:sidxlast] * str2
32+
end
833

934
omit(needle::Nothing, haystack::Nothing) = nothing
1035
omit(needle, haystack::Nothing) = nothing
@@ -51,7 +76,7 @@ function findeach(needle, haystack)
5176
end
5277

5378
function nanodateformat(nd::NanoDate, df::DateFormat; subsecsep::Union{Char,AbstractString}='.')
54-
dfstr = String(df)
79+
dfstr = safestring(df)
5580
scount, sindices = findeach('s', dfstr)
5681
idxsubsecsep = subsecsep == "" ? nothing : findlast(subsecsep, dfstr)
5782
if !isnothing(idxsubsecsep)

src/strings.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ charvec2string(chrs::AbstractVector{Char}) = foldl(*, chrs; init = "")
8282
function nanodate_format(nd, df)
8383
nooffset(df)
8484
indices = indexperiods(df)
85-
dfstr = String(df)
85+
dfstr = safestring(df)
8686
chrs = string2charvec(dfstr)
8787
indices = NamedTuple{(:yr, :mn, :dy, :hr, :mi, :sc, :ss)}(indices) # omit offset field
8888
syms = keys(indices)
@@ -142,7 +142,7 @@ end
142142
subsec_str = lpad(subsec_value, 9, '0')
143143
supersec = datetime - Millisecond(datetime)
144144
145-
dfstr = String(df)
145+
dfstr = safestring(df)
146146
str = Dates.format(datetime, df)
147147
value(nd.nanosecs) == 0 && return str
148148
@@ -195,15 +195,15 @@ function nanodate_format(nd, df, sep)
195195
str
196196
end
197197

198-
nooffset(df::DateFormat) = nooffset(String(df))
198+
nooffset(df::DateFormat) = nooffset(safestring(df))
199199
function nooffset(str::AbstractString)
200200
if occursin('+', str)
201201
throw(ArgumentError("utc offsets are not supported in format(), use timestamp()"))
202202
end
203203
end
204204

205205

206-
separate_offset(df::DateFormat) = separate_offset(String(df))
206+
separate_offset(df::DateFormat) = separate_offset(safestring(df))
207207

208208
function separate_offset(str::AbstractString)
209209
if isempty(str) || isdigit(str[end])
@@ -323,7 +323,7 @@ function getpart(r::UnitRange, str)
323323
end
324324
getpart(x::Nothing, str) = "0"
325325

326-
indexperiods(df::DateFormat) = indexperiods(String(df))
326+
indexperiods(df::DateFormat) = indexperiods(safestring(df))
327327

328328
function indexperiods(dfstr::String)
329329
str = strip(dfstr)

0 commit comments

Comments
 (0)