Skip to content

Commit a7fabc9

Browse files
fchorneyJeffBezanson
authored andcommitted
Fix repr on Period Types, and DateTime, Date (#30817)
- Given `x = Dates.Day(1)` - The REPL will print: `1 day` - show will print: `Dates.Day(1)` - repr will print: `Dates.Day(1)` - print will print: `1 day` - string will print: `1 day` - dataframes will print: `1 day`
1 parent 1c1fc03 commit a7fabc9

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

stdlib/Dates/docs/src/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Date
244244
value: Int64 735264
245245
246246
julia> t.instant
247-
Dates.UTInstant{Day}(735264 days)
247+
Dates.UTInstant{Day}(Day(735264))
248248
249249
julia> Dates.value(t)
250250
735264
@@ -400,7 +400,7 @@ 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-
Date(2014, 1, 29):1 day:Date(2014, 2, 3)
403+
Date(2014, 1, 29):Day(1):Date(2014, 2, 3)
404404
405405
julia> collect(dr)
406406
6-element Array{Date,1}:
@@ -412,7 +412,7 @@ julia> collect(dr)
412412
Date(2014, 2, 3)
413413
414414
julia> dr = Date(2014,1,29):Dates.Month(1):Date(2014,07,29)
415-
Date(2014, 1, 29):1 month:Date(2014, 7, 29)
415+
Date(2014, 1, 29):Month(1):Date(2014, 7, 29)
416416
417417
julia> collect(dr)
418418
7-element Array{Date,1}:

stdlib/Dates/src/io.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ end
529529

530530
# show
531531

532+
function Base.show(io::IO, p::P) where P <: Period
533+
if get(io, :compact, false)
534+
print(io, p)
535+
else
536+
print(io, P, '(', p.value, ')')
537+
end
538+
end
539+
532540
function Base.show(io::IO, dt::DateTime)
533541
if get(io, :compact, false)
534542
print(io, dt)
@@ -539,9 +547,9 @@ function Base.show(io::IO, dt::DateTime)
539547
s = second(dt)
540548
ms = millisecond(dt)
541549
if ms == 0
542-
print(io, "DateTime($y, $m, $d, $h, $mi, $s)")
550+
print(io, DateTime, "($y, $m, $d, $h, $mi, $s)")
543551
else
544-
print(io, "DateTime($y, $m, $d, $h, $mi, $s, $ms)")
552+
print(io, DateTime, "($y, $m, $d, $h, $mi, $s, $ms)")
545553
end
546554
end
547555
end
@@ -559,7 +567,7 @@ function Base.show(io::IO, dt::Date)
559567
print(io, dt)
560568
else
561569
y,m,d = yearmonthday(dt)
562-
print(io, "Date($y, $m, $d)")
570+
print(io, Date, "($y, $m, $d)")
563571
end
564572
end
565573

stdlib/Dates/src/periods.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond
4545
end
4646

4747
#Print/show/traits
48-
Base.string(x::Period) = string(value(x), _units(x))
49-
Base.show(io::IO,x::Period) = print(io, string(x))
48+
Base.print(io::IO, p::Period) = print(io, value(p), _units(p))
49+
Base.show(io::IO, ::MIME"text/plain", p::Period) = print(io, p)
5050
Base.zero(::Union{Type{P},P}) where {P<:Period} = P(0)
5151
Base.one(::Union{Type{P},P}) where {P<:Period} = 1 # see #16116
5252
Base.typemin(::Type{P}) where {P<:Period} = P(typemin(Int64))

stdlib/Dates/test/io.jl

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,61 @@ module IOTests
55
using Test
66
using Dates
77

8+
@testset "string/show representation of Period" begin
9+
@test string(Dates.Year(2018)) == "2018 years"
10+
@test sprint(show, Dates.Year(2018)) == "Dates.Year(2018)"
11+
@test sprint(print, Dates.Year(2018)) == "2018 years"
12+
@test repr(Dates.Year(2018)) == "Dates.Year(2018)"
13+
14+
@test string(Dates.Month(12)) == "12 months"
15+
@test sprint(show, Dates.Month(12)) == "Dates.Month(12)"
16+
@test sprint(print, Dates.Month(12)) == "12 months"
17+
@test repr(Dates.Month(12)) == "Dates.Month(12)"
18+
19+
@test string(Dates.Week(4)) == "4 weeks"
20+
@test sprint(show, Dates.Week(4)) == "Dates.Week(4)"
21+
@test sprint(print, Dates.Week(4)) == "4 weeks"
22+
@test repr(Dates.Week(4)) == "Dates.Week(4)"
23+
24+
@test string(Dates.Day(12)) == "12 days"
25+
@test sprint(show, Dates.Day(12)) == "Dates.Day(12)"
26+
@test sprint(print,Dates.Day(12)) == "12 days"
27+
@test repr(Dates.Day(12)) == "Dates.Day(12)"
28+
29+
@test string(Dates.Hour(12)) == "12 hours"
30+
@test sprint(show, Dates.Hour(12)) == "Dates.Hour(12)"
31+
@test sprint(print,Dates.Hour(12)) == "12 hours"
32+
@test repr(Dates.Hour(12)) == "Dates.Hour(12)"
33+
34+
@test string(Dates.Minute(12)) == "12 minutes"
35+
@test sprint(show, Dates.Minute(12)) == "Dates.Minute(12)"
36+
@test sprint(print,Dates.Minute(12)) == "12 minutes"
37+
@test repr(Dates.Minute(12)) == "Dates.Minute(12)"
38+
39+
@test string(Dates.Second(12)) == "12 seconds"
40+
@test sprint(show, Dates.Second(12)) == "Dates.Second(12)"
41+
@test sprint(print,Dates.Second(12)) == "12 seconds"
42+
@test repr(Dates.Second(12)) == "Dates.Second(12)"
43+
44+
@test string(Dates.Millisecond(12)) == "12 milliseconds"
45+
@test sprint(show, Dates.Millisecond(12)) == "Dates.Millisecond(12)"
46+
@test sprint(print,Dates.Millisecond(12)) == "12 milliseconds"
47+
@test repr(Dates.Millisecond(12)) == "Dates.Millisecond(12)"
48+
49+
@test string(Dates.Microsecond(12)) == "12 microseconds"
50+
@test sprint(show, Dates.Microsecond(12)) == "Dates.Microsecond(12)"
51+
@test sprint(print,Dates.Microsecond(12)) == "12 microseconds"
52+
@test repr(Dates.Microsecond(12)) == "Dates.Microsecond(12)"
53+
54+
@test string(Dates.Nanosecond(12)) == "12 nanoseconds"
55+
@test sprint(show, Dates.Nanosecond(12)) == "Dates.Nanosecond(12)"
56+
@test sprint(print,Dates.Nanosecond(12)) == "12 nanoseconds"
57+
@test repr(Dates.Nanosecond(12)) == "Dates.Nanosecond(12)"
58+
end
59+
860
@testset "string/show representation of Date" begin
961
@test string(Dates.Date(1, 1, 1)) == "0001-01-01" # January 1st, 1 AD/CE
10-
@test sprint(show, Dates.Date(1, 1, 1)) == "Date(1, 1, 1)"
62+
@test sprint(show, Dates.Date(1, 1, 1)) == "Dates.Date(1, 1, 1)"
1163
@test string(Dates.Date(0, 12, 31)) == "0000-12-31" # December 31, 1 BC/BCE
1264
@test Dates.Date(1, 1, 1) - Dates.Date(0, 12, 31) == Dates.Day(1)
1365
@test Dates.Date(Dates.UTD(-306)) == Dates.Date(0, 2, 29)
@@ -16,7 +68,7 @@ using Dates
1668
@test string(Dates.Date(-1000000, 1, 1)) == "-1000000-01-01"
1769
@test string(Dates.Date(1000000, 1, 1)) == "1000000-01-01"
1870
@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)) == "DateTime(2000, 1, 1, 0, 0, 0, 1)"
71+
@test sprint(show, Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)"
2072
@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 2)) == "2000-01-01T00:00:00.002"
2173
@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 500)) == "2000-01-01T00:00:00.5"
2274
@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 998)) == "2000-01-01T00:00:00.998"

0 commit comments

Comments
 (0)