From 32f2432c17b3b671cecdafde9fb792e3c5bfac59 Mon Sep 17 00:00:00 2001 From: Moelf Date: Sun, 30 Aug 2020 00:24:44 -0400 Subject: [PATCH] add rouding for DateTime to Date --- stdlib/Dates/src/rounding.jl | 6 ++++++ stdlib/Dates/test/rounding.jl | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/stdlib/Dates/src/rounding.jl b/stdlib/Dates/src/rounding.jl index 0d77377388618..53e680a6bfd1b 100644 --- a/stdlib/Dates/src/rounding.jl +++ b/stdlib/Dates/src/rounding.jl @@ -284,7 +284,13 @@ Base.round(x::TimeTypeOrPeriod, p::Period) = Base.round(x, p, RoundNearestTiesUp # Make rounding functions callable using Period types in addition to values. Base.floor(x::TimeTypeOrPeriod, ::Type{P}) where P <: Period = Base.floor(x, oneunit(P)) Base.ceil(x::TimeTypeOrPeriod, ::Type{P}) where P <: Period = Base.ceil(x, oneunit(P)) +Base.floor(::Type{Date}, x::TimeTypeOrPeriod, ::Type{P}) where P <: Period = Base.floor(Date(x), oneunit(P)) +Base.ceil(::Type{Date}, x::TimeTypeOrPeriod, ::Type{P}) where P <: Period = Base.ceil(Date(x), oneunit(P)) function Base.round(x::TimeTypeOrPeriod, ::Type{P}, r::RoundingMode=RoundNearestTiesUp) where P <: Period return Base.round(x, oneunit(P), r) end + +function Base.round(::Type{Date}, x::TimeTypeOrPeriod, ::Type{P}, r::RoundingMode=RoundNearestTiesUp) where P <: Period + return Base.round(Date(x), oneunit(P), r) +end diff --git a/stdlib/Dates/test/rounding.jl b/stdlib/Dates/test/rounding.jl index 0227535b46d94..b9fa2fcec42ca 100644 --- a/stdlib/Dates/test/rounding.jl +++ b/stdlib/Dates/test/rounding.jl @@ -188,6 +188,17 @@ end @test round(x, Dates.Microsecond) == Dates.Microsecond(2001000) @test round(x, Dates.Nanosecond) == x end + +@testset "Rouding DateTime to Date" begin + now_ = now() + for p in (Year, Month, Day) + for r in (RoundUp, RoundDown) + @test round(Date, now_, p, r) == round(Date(now_), p, r) + end + @test floor(Date, now_, p) == round(Date, now_, p, RoundDown) + @test ceil(Date, now_, p) == round(Date, now_, p, RoundUp) + end +end @testset "Rounding for periods that should not need rounding" begin for x in [Dates.Week(3), Dates.Day(14), Dates.Second(604800)] local x @@ -229,4 +240,3 @@ end end end -