Skip to content

Commit

Permalink
Respect format width for milliseconds
Browse files Browse the repository at this point in the history
Fixes #21001
  • Loading branch information
omus committed Mar 17, 2017
1 parent b549d2b commit ddf1f33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 6 additions & 6 deletions base/dates/io.jl
Expand Up @@ -154,15 +154,15 @@ end

function format(io, d::DatePart{'s'}, dt)
ms = millisecond(dt)
if ms == 0
write(io, '0')
elseif ms % 100 == 0
write(io, dec(div(ms, 100), 1))
if ms % 100 == 0
str = dec(div(ms, 100), 1)
elseif ms % 10 == 0
write(io, dec(div(ms, 10), 2))
str = dec(div(ms, 10), 2)
else
write(io, dec(ms, 3))
str = dec(ms, 3)
end

write(io, rpad(str, d.width, '0'))
end

### Delimiters
Expand Down
9 changes: 9 additions & 0 deletions test/dates/io.jl
Expand Up @@ -420,3 +420,12 @@ end
@test Dates.Date("Apr 01 2014", "uuu dd yyyy") == Dates.Date(2014, 4, 1)
@test_throws ArgumentError Dates.Date("Apr 01 xx 2014", "uuu dd zz yyyy")
@test_throws ArgumentError Dates.Date("Apr 01 xx 2014", "uuu dd yyyy")

# Issue 21001
for (ms, str) in zip([0, 1, 20, 300, 450, 678], ["0", "001", "02", "3", "45", "678"])
dt = DateTime(2000, 1, 1, 0, 0, 0, ms)
@test Dates.format(dt, "s") == str
@test Dates.format(dt, "ss") == rpad(str, 2, '0')
@test Dates.format(dt, "sss") == rpad(str, 3, '0')
@test Dates.format(dt, "ssss") == rpad(str, 4, '0')
end

0 comments on commit ddf1f33

Please sign in to comment.