diff --git a/NEWS.md b/NEWS.md index ebb6d68faa426..8ab4775c672b3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -27,6 +27,10 @@ Standard library changes * Added keyword arguments `rtol`, `atol` to `pinv` and `nullspace` ([#29998]). +#### Dates + +* Fixed `repr` such that it displays `DateTime` as it would be entered in Julia ([#30200]). + External dependencies --------------------- diff --git a/stdlib/Dates/docs/src/index.md b/stdlib/Dates/docs/src/index.md index bb0a9f2dabf3c..6d5932c1c0569 100644 --- a/stdlib/Dates/docs/src/index.md +++ b/stdlib/Dates/docs/src/index.md @@ -400,29 +400,29 @@ As a bonus, all period arithmetic objects work directly with ranges: ```jldoctest julia> dr = Date(2014,1,29):Day(1):Date(2014,2,3) -2014-01-29:1 day:2014-02-03 +Date(2014, 1, 29):1 day:Date(2014, 2, 3) julia> collect(dr) 6-element Array{Date,1}: - 2014-01-29 - 2014-01-30 - 2014-01-31 - 2014-02-01 - 2014-02-02 - 2014-02-03 + Date(2014, 1, 29) + Date(2014, 1, 30) + Date(2014, 1, 31) + Date(2014, 2, 1) + Date(2014, 2, 2) + Date(2014, 2, 3) julia> dr = Date(2014,1,29):Dates.Month(1):Date(2014,07,29) -2014-01-29:1 month:2014-07-29 +Date(2014, 1, 29):1 month:Date(2014, 7, 29) julia> collect(dr) 7-element Array{Date,1}: - 2014-01-29 - 2014-02-28 - 2014-03-29 - 2014-04-29 - 2014-05-29 - 2014-06-29 - 2014-07-29 + Date(2014, 1, 29) + Date(2014, 2, 28) + Date(2014, 3, 29) + Date(2014, 4, 29) + Date(2014, 5, 29) + Date(2014, 6, 29) + Date(2014, 7, 29) ``` ## Adjuster Functions @@ -492,14 +492,15 @@ julia> filter(dr) do x Dates.dayofweekofmonth(x) == 2 end 8-element Array{Date,1}: - 2014-04-08 - 2014-05-13 - 2014-06-10 - 2014-07-08 - 2014-08-12 - 2014-09-09 - 2014-10-14 - 2014-11-11 + Date(2014, 4, 8) + Date(2014, 5, 13) + Date(2014, 6, 10) + Date(2014, 7, 8) + Date(2014, 8, 12) + Date(2014, 9, 9) + Date(2014, 10, 14) + Date(2014, 11, 11) + ``` Additional examples and tests are available in [`stdlib/Dates/test/adjusters.jl`](https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/test/adjusters.jl). diff --git a/stdlib/Dates/src/io.jl b/stdlib/Dates/src/io.jl index a3d1d036a34f6..3793c86834e88 100644 --- a/stdlib/Dates/src/io.jl +++ b/stdlib/Dates/src/io.jl @@ -530,6 +530,40 @@ end # show function Base.show(io::IO, dt::DateTime) + if get(io, :compact, false) + print(io, dt) + else + y,m,d = yearmonthday(dt) + h = hour(dt) + mi = minute(dt) + s = second(dt) + ms = millisecond(dt) + if ms == 0 + print(io, "DateTime($y, $m, $d, $h, $mi, $s)") + else + print(io, "DateTime($y, $m, $d, $h, $mi, $s, $ms)") + end + end +end + +function Base.show(io::IO, ::MIME"text/plain", dt::DateTime) + print(io, dt) +end + +function Base.show(io::IO, ::MIME"text/plain", dt::Date) + print(io, dt) +end + +function Base.show(io::IO, dt::Date) + if get(io, :compact, false) + print(io, dt) + else + y,m,d = yearmonthday(dt) + print(io, "Date($y, $m, $d)") + end +end + +function Base.print(io::IO, dt::DateTime) if millisecond(dt) == 0 format(io, dt, dateformat"YYYY-mm-dd\THH:MM:SS") else @@ -537,7 +571,7 @@ function Base.show(io::IO, dt::DateTime) end end -function Base.show(io::IO, dt::Date) +function Base.print(io::IO, dt::Date) format(io, dt, dateformat"YYYY-mm-dd") end diff --git a/stdlib/Dates/test/io.jl b/stdlib/Dates/test/io.jl index f238534e30305..acfa092a9f7ef 100644 --- a/stdlib/Dates/test/io.jl +++ b/stdlib/Dates/test/io.jl @@ -7,7 +7,7 @@ using Dates @testset "string/show representation of Date" begin @test string(Dates.Date(1, 1, 1)) == "0001-01-01" # January 1st, 1 AD/CE - @test sprint(show, Dates.Date(1, 1, 1)) == "0001-01-01" + @test sprint(show, Dates.Date(1, 1, 1)) == "Date(1, 1, 1)" @test string(Dates.Date(0, 12, 31)) == "0000-12-31" # December 31, 1 BC/BCE @test Dates.Date(1, 1, 1) - Dates.Date(0, 12, 31) == Dates.Day(1) @test Dates.Date(Dates.UTD(-306)) == Dates.Date(0, 2, 29) @@ -16,7 +16,7 @@ using Dates @test string(Dates.Date(-1000000, 1, 1)) == "-1000000-01-01" @test string(Dates.Date(1000000, 1, 1)) == "1000000-01-01" @test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "2000-01-01T00:00:00.001" - @test sprint(show, Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "2000-01-01T00:00:00.001" + @test sprint(show, Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "DateTime(2000, 1, 1, 0, 0, 0, 1)" @test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 2)) == "2000-01-01T00:00:00.002" @test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 500)) == "2000-01-01T00:00:00.5" @test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 998)) == "2000-01-01T00:00:00.998"