Skip to content

Commit

Permalink
Fix DomainError when parsing DateTime (#22198)
Browse files Browse the repository at this point in the history
* Fix DomainError when parsing DateTime

* Converted tests into a testset

(cherry picked from commit abf3bce)
  • Loading branch information
omus authored and tkelman committed Jun 5, 2017
1 parent 8cd6ad2 commit 7c0a1b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 9 additions & 1 deletion base/dates/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ end
@inline function tryparsenext(d::DatePart{'s'}, str, i, len)
ms, ii = tryparsenext_base10(str, i, len, min_width(d), max_width(d))
if !isnull(ms)
ms = Nullable{Int64}(get(ms) * 10 ^ (3 - (ii - i)))
val = get(ms)
len = ii - i
if len > 3
val, r = divrem(val, Int64(10) ^ (len - 3))
r == 0 || throw(InexactError())
else
val *= Int64(10) ^ (3 - len)
end
ms = Nullable{Int64}(val)
end
return ms, ii
end
Expand Down
13 changes: 13 additions & 0 deletions test/dates/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,16 @@ end

# Issue #21504
@test isnull(tryparse(Dates.Date, "0-1000"))

# Issue #22100
@testset "parse milliseconds" begin
@test Dates.DateTime("2017-Mar-17 00:00:00.0000", "y-u-d H:M:S.s") == Dates.DateTime(2017, 3, 17)
@test Dates.parse_components(".1", Dates.DateFormat(".s")) == [Dates.Millisecond(100)]
@test Dates.parse_components(".12", Dates.DateFormat(".s")) == [Dates.Millisecond(120)]
@test Dates.parse_components(".123", Dates.DateFormat(".s")) == [Dates.Millisecond(123)]
@test Dates.parse_components(".1230", Dates.DateFormat(".s")) == [Dates.Millisecond(123)]
@test_throws InexactError Dates.parse_components(".1234", Dates.DateFormat(".s"))

# Ensure that no overflow occurs when using Int32 literals: Int32(10)^10
@test Dates.parse_components("." * rpad(999, 10, '0'), Dates.DateFormat(".s")) == [Dates.Millisecond(999)]
end

0 comments on commit 7c0a1b7

Please sign in to comment.