Skip to content

Commit

Permalink
Add '!' as marker for fixed width to date formats
Browse files Browse the repository at this point in the history
  • Loading branch information
bug-brain committed Aug 22, 2018
1 parent 755b623 commit 8a48327
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions stdlib/Dates/src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,28 +342,26 @@ function DateFormat(f::AbstractString, locale::DateLocale=ENGLISH)
opt_depth = 0

letters = String(collect(keys(CONVERSION_SPECIFIERS)))
for m in eachmatch(Regex("(?<!\\\\)([\\Q$letters[]\\E])\\1*"), f)
for m in eachmatch(Regex("(?<!\\\\)([\\Q$letters[]\\E])\\1*!?"), f)
tran = replace(f[prev_offset:prevind(f, m.offset)], r"\\(.)" => s"\1")

if !isempty(prev)
opt_depth = _push_token!(tokens, prev, isempty(tran), opt_depth)
opt_depth < 0 && throw(ArgumentError("Unmatched closing optional ']' in $f"))
opt_depth = _push_token!(tokens, f, prev, opt_depth)
end

if !isempty(tran)
push!(tokens, Delim(length(tran) == 1 ? first(tran) : tran))
end

width = length(m.match)
prev = (f[m.offset], width)
prev = (f[m.offset], width, endswith(m.match, '!'))
prev_offset = m.offset + width
end

tran = replace(f[prev_offset:lastindex(f)], r"\\(.)" => s"\1")

if !isempty(prev)
opt_depth = _push_token!(tokens, prev, false, opt_depth)
opt_depth < 0 && throw(ArgumentError("Unmatched closing optional ']' in $f"))
opt_depth = _push_token!(tokens, f, prev, opt_depth)
end

if !isempty(tran)
Expand All @@ -376,14 +374,18 @@ function DateFormat(f::AbstractString, locale::DateLocale=ENGLISH)
return DateFormat{Symbol(f),typeof(tokens_tuple)}(tokens_tuple, locale)
end

function _push_token!(tokens::Vector{AbstractDateToken}, prev::Tuple{Char, Int}, fixed::Bool, depth::Int)
letter, width = prev
function _push_token!(tokens::Vector{AbstractDateToken}, f::AbstractString,
(letter, width, fixed)::Tuple{Char, Int, Bool}, depth::Int)
if fixed
width -= 1
end
if letter == '['
for i in 1:width
push!(tokens, OptionalStart{depth+i}())
end
depth+width
elseif letter == ']'
width > depth && throw(ArgumentError("Unmatched closing optional ']' in $f"))
for i in 0:width-1
push!(tokens, OptionalEnd{depth-i}())
end
Expand Down

0 comments on commit 8a48327

Please sign in to comment.