Truncating conversion to days

julia> Dates.days(Hour(1))

julia> Dates.days(Year(1))

One of these truncates and one does not. What is the rationale behind this distinction?

1 Like
julia> Dates.days(Minute(1))

julia> Dates.days(Month(1))

Hour < Day, Year > Day

Right. Why is that distinction useful? It seems more natural to me that I’d want to round down in both cases or neither.

“how many days are there in a minute?” “0”

it’s not doing this:

julia> convert(Day, Minute(1))
ERROR: InexactError: divexact(Int64, 0.0006944444444444445)

julia> convert(Minute, Day(1))
1440 minutes

which gives you error explicitly if doesn’t convert

Why round down in the >0 case but not the >1 case?
Or, why return a fractional answer in the >1 case but not the >0 case?

Indeed, this seems inconsistent and it could also provide a fraction of days for units smaller than a period of one day but in the source code it is currently defined as:

days(c::Millisecond) = div(value(c),86400000)
days(c::Second)      = div(value(c),86400)
days(c::Minute)      = div(value(c),1440)
days(c::Hour)        = div(value(c),24)

With the current logic, the following is false (distributive property breaks):

days(Minute(12*60)) + days(Minute(12*60)) == days(Minute(24*60))

don’t really see why asking:

how many Days are there in ___

should be distributive in the first place, not all functions that give you numbers should be anyway

julia> floor(0.8) + floor(0.8) == floor(1.6)

I think either way would be fine by me, the current behavior is not bad IMO.