Julia: Floor / Ceil methods for Number and Period Inconsistency

Created on 17 Feb 2020  路  4Comments  路  Source: JuliaLang/julia

I just noticed that the floor / ceil methods for Number and Period have the positional arguments inverted.

floor(Int, 3.5)
using Dates
floor(DateTime("2000-01-31"), Month)

From Slack, Dates should be consistent with Base.

Related: #18574

dates good first issue

Most helpful comment

I'd like to work on this, if I understand this correctly, we'd like the following to pass as tests in the end?

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

All 4 comments

Out of curiosity, what has changed that the arguments against this proposal from #18574 are no longer valid?

I think just people keep getting confused by the order... Would it be too bad to have both methods?

ceil(dt::TimeType, p::Period) -> TimeType
ceil(p::Period, dt::TimeType) -> TimeType
ceil(x::Period, precision::T) where T <: Union{TimePeriod, Week, Day} -> T
ceil(precision::T, x::Period) where T <: Union{TimePeriod, Week, Day} -> T

As I already posted in https://github.com/JuliaLang/julia/pull/34810#issuecomment-593851089 I'm thinking that the original considerations in https://github.com/JuliaLang/julia/issues/18574 were right and the current behaviour is not inconsistent but missing functionality. It can be seen easily if we add a function which can round to a period while converting the time type:

round(Date, now(), Month, RoundUp) == Date(2020, 4) #at least for the month of the post 馃槃

Tl;Dr: rather add those missing functions and make only the 2nd argument a required one

I'd like to work on this, if I understand this correctly, we'd like the following to pass as tests in the end?

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
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sbromberger picture sbromberger  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

omus picture omus  路  3Comments

StefanKarpinski picture StefanKarpinski  路  3Comments

arshpreetsingh picture arshpreetsingh  路  3Comments