Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repr on DateTime #30200

Merged
merged 13 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions stdlib/Dates/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes seems like an unintended consequence? At least to me it is a step back in readability.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the idea is to show dates and times in a format that is valid input syntax. The fact that the step doesn't quite use valid input syntax is the remaining issue here—if that was shown as Day(1) instead then this whole output would be valid input syntax. Printing date ranges could use a more human-readable format instead of a valid input syntax.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, placed the comment on the wrong line; do we want the new printing from the collect on the next lines?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I guess we can use either format there. This seems fine to me though.


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
Expand Down Expand Up @@ -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).
Expand Down
36 changes: 35 additions & 1 deletion stdlib/Dates/src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,48 @@ 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
format(io, dt, dateformat"YYYY-mm-dd\THH:MM:SS.s")
end
end

function Base.show(io::IO, dt::Date)
function Base.print(io::IO, dt::Date)
format(io, dt, dateformat"YYYY-mm-dd")
end

Expand Down
4 changes: 2 additions & 2 deletions stdlib/Dates/test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down