Skip to content

error when using DateTime with string input when year is negative #50328

Closed
@vmpyr

Description

@vmpyr

So I tried to use DateTime("-2011-01-01", "yyyy-mm-dd") and ran into this error:

julia> DateTime("-2011-01-01", "yyyy-mm-dd")
ERROR: ArgumentError: Unable to parse date time. Expected directive DatePart(yyyy) at char 1
Stacktrace:
 [1] macro expansion
   @ ~/.julia/juliaup/julia-1.9.1+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/parse.jl:104 [inlined]
 [2] tryparsenext_core(str::String, pos::Int64, len::Int64, df::DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{'y'}, Dates.Delim{Char, 1}, Dates.DatePart{'m'}, Dates.Delim{Char, 1}, Dates.DatePart{'d'}}}, raise::Bool)
   @ Dates ~/.julia/juliaup/julia-1.9.1+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/parse.jl:38
 [3] macro expansion
   @ ~/.julia/juliaup/julia-1.9.1+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/parse.jl:150 [inlined]
 [4] tryparsenext_internal
   @ ~/.julia/juliaup/julia-1.9.1+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/parse.jl:125 [inlined]
 [5] parse(::Type{DateTime}, str::String, df::DateFormat{Symbol("yyyy-mm-dd"), Tuple{Dates.DatePart{'y'}, Dates.Delim{Char, 1}, Dates.DatePart{'m'}, Dates.Delim{Char, 1}, Dates.DatePart{'d'}}})
   @ Dates ~/.julia/juliaup/julia-1.9.1+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/parse.jl:284
 [6] DateTime(dt::String, format::String; locale::Dates.DateLocale)
   @ Dates ~/.julia/juliaup/julia-1.9.1+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/io.jl:555
 [7] DateTime(dt::String, format::String)
   @ Dates ~/.julia/juliaup/julia-1.9.1+0.x64.linux.gnu/share/julia/stdlib/v1.9/Dates/src/io.jl:554
 [8] top-level scope
   @ REPL[6]:1

The functions works fine if the year isn't negative. I know what I'm trying to do can be achieved by doing this instead:

julia> DateTime(-2011, 01, 01, 0)
-2011-01-01T00:00:00

But shouldn't the former way of doing things work as well?

Julia Verion 1.9.1 installed using juliaup:

julia> versioninfo()
Julia Version 1.9.1
Commit 147bdf428cd (2023-06-07 08:27 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × AMD Ryzen 5 4600H with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, znver2)
  Threads: 1 on 12 virtual cores

Metadata

Metadata

Assignees

No one assigned

    Labels

    datesDates, times, and the Dates stdlib module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions