Skip to content

Commit

Permalink
Fix repr on Period Types, and DateTime, Date (#30817)
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
fchorney authored and JeffBezanson committed Jan 28, 2019
1 parent 1c1fc03 commit a7fabc9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
6 changes: 3 additions & 3 deletions stdlib/Dates/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ Date
value: Int64 735264
julia> t.instant
Dates.UTInstant{Day}(735264 days)
Dates.UTInstant{Day}(Day(735264))
julia> Dates.value(t)
735264
Expand Down Expand Up @@ -400,7 +400,7 @@ As a bonus, all period arithmetic objects work directly with ranges:

```jldoctest
julia> dr = Date(2014,1,29):Day(1):Date(2014,2,3)
Date(2014, 1, 29):1 day:Date(2014, 2, 3)
Date(2014, 1, 29):Day(1):Date(2014, 2, 3)
julia> collect(dr)
6-element Array{Date,1}:
Expand All @@ -412,7 +412,7 @@ julia> collect(dr)
Date(2014, 2, 3)
julia> dr = Date(2014,1,29):Dates.Month(1):Date(2014,07,29)
Date(2014, 1, 29):1 month:Date(2014, 7, 29)
Date(2014, 1, 29):Month(1):Date(2014, 7, 29)
julia> collect(dr)
7-element Array{Date,1}:
Expand Down
14 changes: 11 additions & 3 deletions stdlib/Dates/src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,14 @@ end

# show

function Base.show(io::IO, p::P) where P <: Period
if get(io, :compact, false)
print(io, p)
else
print(io, P, '(', p.value, ')')
end
end

function Base.show(io::IO, dt::DateTime)
if get(io, :compact, false)
print(io, dt)
Expand All @@ -539,9 +547,9 @@ function Base.show(io::IO, dt::DateTime)
s = second(dt)
ms = millisecond(dt)
if ms == 0
print(io, "DateTime($y, $m, $d, $h, $mi, $s)")
print(io, DateTime, "($y, $m, $d, $h, $mi, $s)")
else
print(io, "DateTime($y, $m, $d, $h, $mi, $s, $ms)")
print(io, DateTime, "($y, $m, $d, $h, $mi, $s, $ms)")
end
end
end
Expand All @@ -559,7 +567,7 @@ function Base.show(io::IO, dt::Date)
print(io, dt)
else
y,m,d = yearmonthday(dt)
print(io, "Date($y, $m, $d)")
print(io, Date, "($y, $m, $d)")
end
end

Expand Down
4 changes: 2 additions & 2 deletions stdlib/Dates/src/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond
end

#Print/show/traits
Base.string(x::Period) = string(value(x), _units(x))
Base.show(io::IO,x::Period) = print(io, string(x))
Base.print(io::IO, p::Period) = print(io, value(p), _units(p))
Base.show(io::IO, ::MIME"text/plain", p::Period) = print(io, p)
Base.zero(::Union{Type{P},P}) where {P<:Period} = P(0)
Base.one(::Union{Type{P},P}) where {P<:Period} = 1 # see #16116
Base.typemin(::Type{P}) where {P<:Period} = P(typemin(Int64))
Expand Down
56 changes: 54 additions & 2 deletions stdlib/Dates/test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,61 @@ module IOTests
using Test
using Dates

@testset "string/show representation of Period" begin
@test string(Dates.Year(2018)) == "2018 years"
@test sprint(show, Dates.Year(2018)) == "Dates.Year(2018)"
@test sprint(print, Dates.Year(2018)) == "2018 years"
@test repr(Dates.Year(2018)) == "Dates.Year(2018)"

@test string(Dates.Month(12)) == "12 months"
@test sprint(show, Dates.Month(12)) == "Dates.Month(12)"
@test sprint(print, Dates.Month(12)) == "12 months"
@test repr(Dates.Month(12)) == "Dates.Month(12)"

@test string(Dates.Week(4)) == "4 weeks"
@test sprint(show, Dates.Week(4)) == "Dates.Week(4)"
@test sprint(print, Dates.Week(4)) == "4 weeks"
@test repr(Dates.Week(4)) == "Dates.Week(4)"

@test string(Dates.Day(12)) == "12 days"
@test sprint(show, Dates.Day(12)) == "Dates.Day(12)"
@test sprint(print,Dates.Day(12)) == "12 days"
@test repr(Dates.Day(12)) == "Dates.Day(12)"

@test string(Dates.Hour(12)) == "12 hours"
@test sprint(show, Dates.Hour(12)) == "Dates.Hour(12)"
@test sprint(print,Dates.Hour(12)) == "12 hours"
@test repr(Dates.Hour(12)) == "Dates.Hour(12)"

@test string(Dates.Minute(12)) == "12 minutes"
@test sprint(show, Dates.Minute(12)) == "Dates.Minute(12)"
@test sprint(print,Dates.Minute(12)) == "12 minutes"
@test repr(Dates.Minute(12)) == "Dates.Minute(12)"

@test string(Dates.Second(12)) == "12 seconds"
@test sprint(show, Dates.Second(12)) == "Dates.Second(12)"
@test sprint(print,Dates.Second(12)) == "12 seconds"
@test repr(Dates.Second(12)) == "Dates.Second(12)"

@test string(Dates.Millisecond(12)) == "12 milliseconds"
@test sprint(show, Dates.Millisecond(12)) == "Dates.Millisecond(12)"
@test sprint(print,Dates.Millisecond(12)) == "12 milliseconds"
@test repr(Dates.Millisecond(12)) == "Dates.Millisecond(12)"

@test string(Dates.Microsecond(12)) == "12 microseconds"
@test sprint(show, Dates.Microsecond(12)) == "Dates.Microsecond(12)"
@test sprint(print,Dates.Microsecond(12)) == "12 microseconds"
@test repr(Dates.Microsecond(12)) == "Dates.Microsecond(12)"

@test string(Dates.Nanosecond(12)) == "12 nanoseconds"
@test sprint(show, Dates.Nanosecond(12)) == "Dates.Nanosecond(12)"
@test sprint(print,Dates.Nanosecond(12)) == "12 nanoseconds"
@test repr(Dates.Nanosecond(12)) == "Dates.Nanosecond(12)"
end

@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)) == "Date(1, 1, 1)"
@test sprint(show, Dates.Date(1, 1, 1)) == "Dates.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)
Expand All @@ -16,7 +68,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)) == "DateTime(2000, 1, 1, 0, 0, 0, 1)"
@test sprint(show, Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "Dates.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"
Expand Down

0 comments on commit a7fabc9

Please sign in to comment.