Skip to content

Conversation

Sr0liveira02
Copy link
Contributor

@Sr0liveira02 Sr0liveira02 commented Apr 6, 2024

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.

Comment on lines 625 to 630
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?

@vtjnash vtjnash changed the title Fix #50328: Date Parsing crahses when parsing a negative year Fix #50328: Date Parsing crashes when parsing a negative year Apr 6, 2024
@vtjnash vtjnash changed the title Fix #50328: Date Parsing crashes when parsing a negative year Fix Date parsing crash when parsing a negative year Apr 6, 2024
stdlib/Dates/test/io.jl

Co-authored-by: Jameson Nash <[email protected]>
@giordano giordano added the dates Dates, times, and the Dates stdlib module label Apr 6, 2024
@ViralBShah
Copy link
Member

Bump.

@Sr0liveira02
Copy link
Contributor Author

Sr0liveira02 commented May 18, 2024

Hi, sorry for the delay.
I understand that the test might have been unnecessary so I removed it.
Thank you so much for the help.

@ViralBShah
Copy link
Member

@quinnj Good to merge?

@quinnj quinnj merged commit 4366a93 into JuliaLang:master May 21, 2024
quinnj pushed a commit that referenced this pull request May 21, 2024
Follow up to #53981. Fixes an issue introduced with negative years and
fixed-with date formats:

```julia
julia> Dates.DateTime("-20240521", "yyyymmdd")
ERROR: ArgumentError: Month: 40 out of range (1:12)
Stacktrace:
 [1] DateTime(y::Int64, m::Int64, d::Int64, h::Int64, mi::Int64, s::Int64, ms::Int64, ampm::Dates.AMPM)
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/types.jl:246
 [2] parse(::Type{DateTime}, str::String, df::DateFormat{:yyyymmdd, Tuple{Dates.DatePart{'y'}, Dates.DatePart{'m'}, Dates.DatePart{'d'}}})
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/parse.jl:294
 [3] DateTime(dt::String, format::String; locale::Dates.DateLocale)
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/io.jl:555
 [4] DateTime(dt::String, format::String)
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/io.jl:554
 [5] top-level scope
   @ REPL[4]:1
```

This PR makes it so that fixed-width formats require the specified
number of digits. I also decided to only add the sign parsing for years
to running into performance issues with parsing sign information where
it isn't expected.
lazarusA pushed a commit to lazarusA/julia that referenced this pull request Jul 12, 2024
Fixes JuliaLang#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]>
lazarusA pushed a commit to lazarusA/julia that referenced this pull request Jul 12, 2024
Follow up to JuliaLang#53981. Fixes an issue introduced with negative years and
fixed-with date formats:

```julia
julia> Dates.DateTime("-20240521", "yyyymmdd")
ERROR: ArgumentError: Month: 40 out of range (1:12)
Stacktrace:
 [1] DateTime(y::Int64, m::Int64, d::Int64, h::Int64, mi::Int64, s::Int64, ms::Int64, ampm::Dates.AMPM)
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/types.jl:246
 [2] parse(::Type{DateTime}, str::String, df::DateFormat{:yyyymmdd, Tuple{Dates.DatePart{'y'}, Dates.DatePart{'m'}, Dates.DatePart{'d'}}})
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/parse.jl:294
 [3] DateTime(dt::String, format::String; locale::Dates.DateLocale)
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/io.jl:555
 [4] DateTime(dt::String, format::String)
   @ Dates ~/Development/Julia/aarch64/latest/usr/share/julia/stdlib/v1.12/Dates/src/io.jl:554
 [5] top-level scope
   @ REPL[4]:1
```

This PR makes it so that fixed-width formats require the specified
number of digits. I also decided to only add the sign parsing for years
to running into performance issues with parsing sign information where
it isn't expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dates Dates, times, and the Dates stdlib module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error when using DateTime with string input when year is negative

5 participants