Skip to content

Commit f552fb2

Browse files
pfarndtStefanKarpinski
authored andcommitted
Add methods min, max, minmax taking only single Dates.AbstractTime (#32525)
Methods `min`, `max` and `minmax` should also be able to take just one `Dates.AbtractTime` argument. See discussion: https://discourse.julialang.org/t/max-and-min-of-single-dates/26074
1 parent 33a6b99 commit f552fb2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

stdlib/Dates/src/types.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ function ==(a::Time, b::Time)
403403
microsecond(a) == microsecond(b) && nanosecond(a) == nanosecond(b)
404404
end
405405
(==)(x::TimeType, y::TimeType) = (===)(promote(x, y)...)
406+
Base.min(x::AbstractTime) = x
407+
Base.max(x::AbstractTime) = x
408+
Base.minmax(x::AbstractTime) = (x, x)
406409
Base.hash(x::Time, h::UInt) =
407410
hash(hour(x), hash(minute(x), hash(second(x),
408411
hash(millisecond(x), hash(microsecond(x), hash(nanosecond(x), h))))))

stdlib/Dates/test/types.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@ end
228228
@test Dates.Date(Dates.DateTime(Dates.Date(2012, 7, 1))) == Dates.Date(2012, 7, 1)
229229
end
230230

231+
@testset "min and max" begin
232+
for (a, b) in [(Dates.Date(2000), Dates.Date(2001)),
233+
(Dates.Time(10), Dates.Time(11)),
234+
(Dates.DateTime(3000), Dates.DateTime(3001)),
235+
(Dates.Week(42), Dates.Week(1972))]
236+
@test min(a, b) == a
237+
@test min(b, a) == a
238+
@test min(a) == a
239+
@test max(a, b) == b
240+
@test max(b, a) == b
241+
@test max(b) == b
242+
@test minmax(a, b) == (a, b)
243+
@test minmax(b, a) == (a, b)
244+
@test minmax(a) == (a, a)
245+
end
246+
end
247+
231248
@testset "issue #31524" begin
232249
dt1 = Libc.strptime("%Y-%M-%dT%H:%M:%SZ", "2018-11-16T10:26:14Z")
233250
dt2 = Base.Libc.TmStruct(14, 30, 5, 10, 1, 99, 3, 40, 0)

0 commit comments

Comments
 (0)