Skip to content

Commit 5f176e0

Browse files
fchorneyJeffBezanson
authored andcommitted
Make Date/Time Printing Consistent (#34387)
- Use 3 arg compact show for array elements. - Add show/print methods for periods - Periods imply their typeinfo, so set typeinfo_implicit to true for all period types fixes #30901
1 parent fba188c commit 5f176e0

File tree

12 files changed

+68
-58
lines changed

12 files changed

+68
-58
lines changed

base/abstractdict.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ Dict{Char,Int64} with 2 entries:
9292
9393
julia> collect(keys(D))
9494
2-element Array{Char,1}:
95-
'a'
96-
'b'
95+
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
96+
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
9797
```
9898
"""
9999
keys(a::AbstractDict) = KeySet(a)

base/arrayshow.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ function print_matrix_row(io::IO,
101101
if isassigned(X,Int(i),Int(j)) # isassigned accepts only `Int` indices
102102
x = X[i,j]
103103
a = alignment(io, x)
104-
sx = sprint(show, x, context=io, sizehint=0)
104+
105+
# First try 3-arg show
106+
sx = sprint(show, "text/plain", x, context=io, sizehint=0)
107+
108+
# If the output contains line breaks, try 2-arg show instead.
109+
if occursin('\n', sx)
110+
sx = sprint(show, x, context=io, sizehint=0)
111+
end
105112
else
106113
a = undef_ref_alignment
107114
sx = undef_ref_str
@@ -317,6 +324,7 @@ function show(io::IO, ::MIME"text/plain", X::AbstractArray)
317324
summary(io, X)
318325
isempty(X) && return
319326
print(io, ":")
327+
show_circular(io, X) && return
320328

321329
# 1) compute new IOContext
322330
if !haskey(io, :compact) && length(axes(X, 2)) > 1
@@ -343,7 +351,8 @@ function show(io::IO, ::MIME"text/plain", X::AbstractArray)
343351
io = IOContext(io, :typeinfo => eltype(X))
344352

345353
# 2) show actual content
346-
print_array(io, X)
354+
recur_io = IOContext(io, :SHOWN_SET => X)
355+
print_array(recur_io, X)
347356
end
348357

349358
## printing with `show`
@@ -448,6 +457,7 @@ function show_vector(io::IO, v, opn='[', cls=']')
448457
# directly or indirectly, the context now knows about eltype(v)
449458
io = IOContext(io, :typeinfo => eltype(v))
450459
limited = get(io, :limit, false)
460+
451461
if limited && length(v) > 20
452462
axs1 = axes1(v)
453463
f, l = first(axs1), last(axs1)

base/combinatorics.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ julia> A = ['a','b','c','d'];
213213
214214
julia> B = A[v]
215215
4-element Array{Char,1}:
216-
'b'
217-
'd'
218-
'c'
219-
'a'
216+
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
217+
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
218+
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
219+
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
220220
221221
julia> B[invperm(v)]
222222
4-element Array{Char,1}:
223-
'a'
224-
'b'
225-
'c'
226-
'd'
223+
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
224+
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
225+
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
226+
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
227227
```
228228
"""
229229
function invperm(a::AbstractVector)

base/iterators.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ julia> a
534534
535535
julia> collect(rest)
536536
2-element Array{Char,1}:
537-
'b'
538-
'c'
537+
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
538+
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
539539
```
540540
"""
541541
function peel(itr)
@@ -1179,14 +1179,14 @@ julia> popfirst!(a)
11791179
11801180
julia> collect(Iterators.take(a, 3))
11811181
3-element Array{Char,1}:
1182-
'b'
1183-
'c'
1184-
'd'
1182+
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
1183+
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
1184+
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
11851185
11861186
julia> collect(a)
11871187
2-element Array{Char,1}:
1188-
'e'
1189-
'f'
1188+
'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
1189+
'f': ASCII/Unicode U+0066 (category Ll: Letter, lowercase)
11901190
```
11911191
11921192
```jldoctest

base/operators.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,9 @@ and splatting `∘(fs...)` for composing an iterable collection of functions.
844844
```jldoctest
845845
julia> map(uppercase∘first, ["apple", "banana", "carrot"])
846846
3-element Array{Char,1}:
847-
'A'
848-
'B'
849-
'C'
847+
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
848+
'B': ASCII/Unicode U+0042 (category Lu: Letter, uppercase)
849+
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
850850
851851
julia> fs = [
852852
x -> 2x

doc/src/manual/functions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,12 @@ The next example composes three functions and maps the result over an array of s
737737
```jldoctest
738738
julia> map(first ∘ reverse ∘ uppercase, split("you can compose functions like this"))
739739
6-element Array{Char,1}:
740-
'U'
741-
'N'
742-
'E'
743-
'S'
744-
'E'
745-
'S'
740+
'U': ASCII/Unicode U+0055 (category Lu: Letter, uppercase)
741+
'N': ASCII/Unicode U+004E (category Lu: Letter, uppercase)
742+
'E': ASCII/Unicode U+0045 (category Lu: Letter, uppercase)
743+
'S': ASCII/Unicode U+0053 (category Lu: Letter, uppercase)
744+
'E': ASCII/Unicode U+0045 (category Lu: Letter, uppercase)
745+
'S': ASCII/Unicode U+0053 (category Lu: Letter, uppercase)
746746
```
747747

748748
Function chaining (sometimes called "piping" or "using a pipe" to send data to a subsequent function) is when you apply a function to the previous function's output:

doc/src/manual/performance-tips.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ julia> push!(a, 1); push!(a, 2.0); push!(a, π)
166166
3-element Array{Real,1}:
167167
1
168168
2.0
169-
π
169+
π = 3.1415926535897...
170170
```
171171

172172
Because `a` is a an array of abstract type [`Real`](@ref), it must be able to hold any

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-
2014-01-29:1 day:2014-02-03
403+
Date("2014-01-29"):Day(1):Date("2014-02-03")
404404
405405
julia> collect(dr)
406406
6-element Array{Date,1}:
@@ -412,7 +412,7 @@ julia> collect(dr)
412412
2014-02-03
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-01-29"):Month(1):Date("2014-07-29")
416416
417417
julia> collect(dr)
418418
7-element Array{Date,1}:

stdlib/Dates/src/io.jl

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -578,33 +578,30 @@ function format(dt::TimeType, f::AbstractString; locale::Locale=ENGLISH)
578578
end
579579

580580
# show
581-
582-
function Base.show(io::IO, dt::DateTime)
583-
if millisecond(dt) == 0
584-
format(io, dt, dateformat"YYYY-mm-dd\THH:MM:SS")
585-
else
586-
format(io, dt, dateformat"YYYY-mm-dd\THH:MM:SS.s")
587-
end
588-
end
589-
590-
function Base.show(io::IO, dt::Date)
591-
format(io, dt, dateformat"YYYY-mm-dd")
592-
end
593-
594-
function Base.string(dt::DateTime)
595-
if millisecond(dt) == 0
581+
function Base.print(io::IO, dt::DateTime)
582+
str = if millisecond(dt) == 0
596583
format(dt, dateformat"YYYY-mm-dd\THH:MM:SS", 24)
597584
else
598585
format(dt, dateformat"YYYY-mm-dd\THH:MM:SS.s", 26)
599586
end
587+
print(io, str)
600588
end
601589

602-
function Base.string(dt::Date)
590+
function Base.print(io::IO, dt::Date)
603591
# don't use format - bypassing IOBuffer creation
604592
# saves a bit of time here.
605593
y,m,d = yearmonthday(value(dt))
606594
yy = y < 0 ? @sprintf("%05i", y) : lpad(y, 4, "0")
607595
mm = lpad(m, 2, "0")
608596
dd = lpad(d, 2, "0")
609-
return "$yy-$mm-$dd"
597+
print(io, "$yy-$mm-$dd")
598+
end
599+
600+
for date_type in (:Date, :DateTime)
601+
# Human readable output (i.e. "2012-01-01")
602+
@eval Base.show(io::IO, ::MIME"text/plain", dt::$date_type) = print(io, dt)
603+
# Parsable output (i.e. Date("2012-01-01"))
604+
@eval Base.show(io::IO, dt::$date_type) = print(io, typeof(dt), "(\"", dt, "\")")
605+
# Parsable output will have type info displayed, thus it is implied
606+
@eval Base.typeinfo_implicit(::Type{$date_type}) = true
610607
end

stdlib/Dates/src/periods.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond
2121
@eval periodisless(x::$period, y::$period) = value(x) < value(y)
2222
# AbstractString parsing (mainly for IO code)
2323
@eval $period(x::AbstractString) = $period(Base.parse(Int64, x))
24+
# The period type is printed when output, thus it already implies its own typeinfo
25+
@eval Base.typeinfo_implicit(::Type{$period}) = true
2426
# Period accessors
2527
typs = period in (:Microsecond, :Nanosecond) ? ["Time"] :
2628
period in (:Hour, :Minute, :Second, :Millisecond) ? ["Time", "DateTime"] : ["Date", "DateTime"]
@@ -45,8 +47,9 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond
4547
end
4648

4749
#Print/show/traits
48-
Base.string(x::Period) = string(value(x), _units(x))
49-
Base.show(io::IO,x::Period) = print(io, string(x))
50+
Base.print(io::IO, x::Period) = print(io, value(x), _units(x))
51+
Base.show(io::IO, ::MIME"text/plain", x::Period) = print(io, x)
52+
Base.show(io::IO, p::P) where {P<:Period} = print(io, P, '(', value(p), ')')
5053
Base.zero(::Union{Type{P},P}) where {P<:Period} = P(0)
5154
Base.one(::Union{Type{P},P}) where {P<:Period} = 1 # see #16116
5255
Base.typemin(::Type{P}) where {P<:Period} = P(typemin(Int64))

0 commit comments

Comments
 (0)