Skip to content

Commit 8d8b3d9

Browse files
sam0410omus
authored andcommitted
Fix repr on DateTime (#30200)
* Add print * Add show function * modify tests * Adding changes in docs * readability * add compact case * revert compact case * compact case * typo * add news * Update NEWS.md
1 parent a0b7a76 commit 8d8b3d9

File tree

4 files changed

+65
-26
lines changed

4 files changed

+65
-26
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Standard library changes
3333

3434
* Added keyword arguments `rtol`, `atol` to `pinv` and `nullspace` ([#29998]).
3535

36+
#### Dates
37+
38+
* Fixed `repr` such that it displays `DateTime` as it would be entered in Julia ([#30200]).
39+
3640

3741
External dependencies
3842
---------------------

stdlib/Dates/docs/src/index.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -400,29 +400,29 @@ As a bonus, all period arithmetic objects work directly with ranges:
400400

401401
```jldoctest
402402
julia> dr = Date(2014,1,29):Day(1):Date(2014,2,3)
403-
2014-01-29:1 day:2014-02-03
403+
Date(2014, 1, 29):1 day:Date(2014, 2, 3)
404404
405405
julia> collect(dr)
406406
6-element Array{Date,1}:
407-
2014-01-29
408-
2014-01-30
409-
2014-01-31
410-
2014-02-01
411-
2014-02-02
412-
2014-02-03
407+
Date(2014, 1, 29)
408+
Date(2014, 1, 30)
409+
Date(2014, 1, 31)
410+
Date(2014, 2, 1)
411+
Date(2014, 2, 2)
412+
Date(2014, 2, 3)
413413
414414
julia> dr = Date(2014,1,29):Dates.Month(1):Date(2014,07,29)
415-
2014-01-29:1 month:2014-07-29
415+
Date(2014, 1, 29):1 month:Date(2014, 7, 29)
416416
417417
julia> collect(dr)
418418
7-element Array{Date,1}:
419-
2014-01-29
420-
2014-02-28
421-
2014-03-29
422-
2014-04-29
423-
2014-05-29
424-
2014-06-29
425-
2014-07-29
419+
Date(2014, 1, 29)
420+
Date(2014, 2, 28)
421+
Date(2014, 3, 29)
422+
Date(2014, 4, 29)
423+
Date(2014, 5, 29)
424+
Date(2014, 6, 29)
425+
Date(2014, 7, 29)
426426
```
427427

428428
## Adjuster Functions
@@ -492,14 +492,15 @@ julia> filter(dr) do x
492492
Dates.dayofweekofmonth(x) == 2
493493
end
494494
8-element Array{Date,1}:
495-
2014-04-08
496-
2014-05-13
497-
2014-06-10
498-
2014-07-08
499-
2014-08-12
500-
2014-09-09
501-
2014-10-14
502-
2014-11-11
495+
Date(2014, 4, 8)
496+
Date(2014, 5, 13)
497+
Date(2014, 6, 10)
498+
Date(2014, 7, 8)
499+
Date(2014, 8, 12)
500+
Date(2014, 9, 9)
501+
Date(2014, 10, 14)
502+
Date(2014, 11, 11)
503+
503504
```
504505

505506
Additional examples and tests are available in [`stdlib/Dates/test/adjusters.jl`](https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/test/adjusters.jl).

stdlib/Dates/src/io.jl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,48 @@ end
530530
# show
531531

532532
function Base.show(io::IO, dt::DateTime)
533+
if get(io, :compact, false)
534+
print(io, dt)
535+
else
536+
y,m,d = yearmonthday(dt)
537+
h = hour(dt)
538+
mi = minute(dt)
539+
s = second(dt)
540+
ms = millisecond(dt)
541+
if ms == 0
542+
print(io, "DateTime($y, $m, $d, $h, $mi, $s)")
543+
else
544+
print(io, "DateTime($y, $m, $d, $h, $mi, $s, $ms)")
545+
end
546+
end
547+
end
548+
549+
function Base.show(io::IO, ::MIME"text/plain", dt::DateTime)
550+
print(io, dt)
551+
end
552+
553+
function Base.show(io::IO, ::MIME"text/plain", dt::Date)
554+
print(io, dt)
555+
end
556+
557+
function Base.show(io::IO, dt::Date)
558+
if get(io, :compact, false)
559+
print(io, dt)
560+
else
561+
y,m,d = yearmonthday(dt)
562+
print(io, "Date($y, $m, $d)")
563+
end
564+
end
565+
566+
function Base.print(io::IO, dt::DateTime)
533567
if millisecond(dt) == 0
534568
format(io, dt, dateformat"YYYY-mm-dd\THH:MM:SS")
535569
else
536570
format(io, dt, dateformat"YYYY-mm-dd\THH:MM:SS.s")
537571
end
538572
end
539573

540-
function Base.show(io::IO, dt::Date)
574+
function Base.print(io::IO, dt::Date)
541575
format(io, dt, dateformat"YYYY-mm-dd")
542576
end
543577

stdlib/Dates/test/io.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using Dates
77

88
@testset "string/show representation of Date" begin
99
@test string(Dates.Date(1, 1, 1)) == "0001-01-01" # January 1st, 1 AD/CE
10-
@test sprint(show, Dates.Date(1, 1, 1)) == "0001-01-01"
10+
@test sprint(show, Dates.Date(1, 1, 1)) == "Date(1, 1, 1)"
1111
@test string(Dates.Date(0, 12, 31)) == "0000-12-31" # December 31, 1 BC/BCE
1212
@test Dates.Date(1, 1, 1) - Dates.Date(0, 12, 31) == Dates.Day(1)
1313
@test Dates.Date(Dates.UTD(-306)) == Dates.Date(0, 2, 29)
@@ -16,7 +16,7 @@ using Dates
1616
@test string(Dates.Date(-1000000, 1, 1)) == "-1000000-01-01"
1717
@test string(Dates.Date(1000000, 1, 1)) == "1000000-01-01"
1818
@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "2000-01-01T00:00:00.001"
19-
@test sprint(show, Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "2000-01-01T00:00:00.001"
19+
@test sprint(show, Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "DateTime(2000, 1, 1, 0, 0, 0, 1)"
2020
@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 2)) == "2000-01-01T00:00:00.002"
2121
@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 500)) == "2000-01-01T00:00:00.5"
2222
@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 998)) == "2000-01-01T00:00:00.998"

0 commit comments

Comments
 (0)