Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Date parsing crash when parsing a negative year #53981

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion stdlib/Dates/src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ end
min_pos = min_width <= 0 ? i : i + min_width - 1
max_pos = max_width <= 0 ? len : min(i + max_width - 1, len)
d::Int64 = 0
c, neg = iterate(str, i)::Tuple{Char, Int}
if c == '-'
i = neg
neg = -1
else
neg = 1
end
@inbounds while i <= max_pos
c, ii = iterate(str, i)::Tuple{Char, Int}
if '0' <= c <= '9'
Expand All @@ -173,7 +180,7 @@ end
if i <= min_pos
return nothing
else
return d, i
return d * neg, i
end
end

Expand Down
12 changes: 12 additions & 0 deletions stdlib/Dates/test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,16 @@ end
end
end

@testset "Issue #50328: parsing negative years" begin
@test Date("-2013-10-10") == Date(-2013, 10, 10)
@test Date("-2013") == Date(-2013, 01, 01)

try
Date("")
@test false
catch err
@test err.msg == "Cannot parse an empty string as a Date or Time"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary?

Sr0liveira02 marked this conversation as resolved.
Show resolved Hide resolved
end

end