Skip to content

Commit

Permalink
Merge pull request #37286 from Moelf/floor_ceil_datetime
Browse files Browse the repository at this point in the history
add rouding for DateTime to Date
  • Loading branch information
quinnj committed Sep 11, 2020
2 parents 0d7dc96 + 73bf549 commit 60c36f3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions stdlib/Dates/src/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion stdlib/Dates/test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ 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 round(Date, now_, p) == round(Date, now_, p, RoundUp)
@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
Expand Down Expand Up @@ -229,4 +241,3 @@ end
end

end

2 comments on commit 60c36f3

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.