Skip to content

Commit 7c0a1b7

Browse files
omustkelman
authored andcommitted
Fix DomainError when parsing DateTime (#22198)
* Fix DomainError when parsing DateTime * Converted tests into a testset (cherry picked from commit abf3bce)
1 parent 8cd6ad2 commit 7c0a1b7

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

base/dates/io.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@ end
111111
@inline function tryparsenext(d::DatePart{'s'}, str, i, len)
112112
ms, ii = tryparsenext_base10(str, i, len, min_width(d), max_width(d))
113113
if !isnull(ms)
114-
ms = Nullable{Int64}(get(ms) * 10 ^ (3 - (ii - i)))
114+
val = get(ms)
115+
len = ii - i
116+
if len > 3
117+
val, r = divrem(val, Int64(10) ^ (len - 3))
118+
r == 0 || throw(InexactError())
119+
else
120+
val *= Int64(10) ^ (3 - len)
121+
end
122+
ms = Nullable{Int64}(val)
115123
end
116124
return ms, ii
117125
end

test/dates/io.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,16 @@ end
432432

433433
# Issue #21504
434434
@test isnull(tryparse(Dates.Date, "0-1000"))
435+
436+
# Issue #22100
437+
@testset "parse milliseconds" begin
438+
@test Dates.DateTime("2017-Mar-17 00:00:00.0000", "y-u-d H:M:S.s") == Dates.DateTime(2017, 3, 17)
439+
@test Dates.parse_components(".1", Dates.DateFormat(".s")) == [Dates.Millisecond(100)]
440+
@test Dates.parse_components(".12", Dates.DateFormat(".s")) == [Dates.Millisecond(120)]
441+
@test Dates.parse_components(".123", Dates.DateFormat(".s")) == [Dates.Millisecond(123)]
442+
@test Dates.parse_components(".1230", Dates.DateFormat(".s")) == [Dates.Millisecond(123)]
443+
@test_throws InexactError Dates.parse_components(".1234", Dates.DateFormat(".s"))
444+
445+
# Ensure that no overflow occurs when using Int32 literals: Int32(10)^10
446+
@test Dates.parse_components("." * rpad(999, 10, '0'), Dates.DateFormat(".s")) == [Dates.Millisecond(999)]
447+
end

0 commit comments

Comments
 (0)