Skip to content

Commit 4366a93

Browse files
Sr0liveira02vtjnashViralBShah
authored
Fix Date parsing crash when parsing a negative year (#53981)
Fixes #50328. Originally checked on version 1.10.0 but still relevant in the current version in master Bug: In method Date(str), when given a negative year as an argument (ex: "-2000"), it would output an error while in Date(int) once given a negative year (ex: "-2000") it would work as intended returning "-2000-01-01". Fix: In function tryparsenext_base10 the character '-' wasn't being accounted so i check if it existed in the first iteration of the string and if yes I would multiply the output * -1. Test: Added some tests that verify this specific Fix. --------- Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Viral B. Shah <[email protected]>
1 parent 98f3947 commit 4366a93

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

stdlib/Dates/src/parse.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ end
161161
min_pos = min_width <= 0 ? i : i + min_width - 1
162162
max_pos = max_width <= 0 ? len : min(i + max_width - 1, len)
163163
d::Int64 = 0
164+
c, neg = iterate(str, i)::Tuple{Char, Int}
165+
if c == '-'
166+
i = neg
167+
neg = -1
168+
else
169+
neg = 1
170+
end
164171
@inbounds while i <= max_pos
165172
c, ii = iterate(str, i)::Tuple{Char, Int}
166173
if '0' <= c <= '9'
@@ -173,7 +180,7 @@ end
173180
if i <= min_pos
174181
return nothing
175182
else
176-
return d, i
183+
return d * neg, i
177184
end
178185
end
179186

stdlib/Dates/test/io.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,9 @@ end
621621
end
622622
end
623623

624+
@testset "Issue #50328: parsing negative years" begin
625+
@test Date("-2013-10-10") == Date(-2013, 10, 10)
626+
@test Date("-2013") == Date(-2013, 01, 01)
627+
end
628+
624629
end

0 commit comments

Comments
 (0)