Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Easier division of interval? #36

Open
ryofurue opened this issue Sep 18, 2023 · 0 comments
Open

Easier division of interval? #36

ryofurue opened this issue Sep 18, 2023 · 0 comments

Comments

@ryofurue
Copy link

I may well be missing something but it seems that currently you can't do this:

using Dates
t1 = DateTime(2017,7,1)
t2 = DateTime(2017,7,1,6)
tm = t1 + (t2 - t1)/7  # 1/7 of the way from t1 to t2

I need to do this a lot to interpolate times. As far as I can tell, the following is the only robust way to do it:

tm = t1 + Millisecond(round((t2 - t1) / Millisecond(1))/ 7))

I know the integer division (÷) works, but it doesn't do rounding.

Intervals behave like an integer, but for the actual integer, the following method is standard:

t1 = 0
t2 = 6
tm = t1 + round(Int, (t2 - t1)/7)

This works because Int/Int results in Float64.

This discussion suggests that we perhaps need a special division function:

divround(i::Millisecond, x::Real, roundingmode = RoundNearest) =
   Millisecond( round(i / Millisecond(1) / x, roundingmode) )
tm = t1 + divround(t2 - t1, 7)

In this way, the user can remain ignorant of the fact that Millisecond is the best unit to divide an interval (so that if/when DateTime starts to include microseconds, the user doesn't have to change her code).

Or perhaps we want to create a FloatingpointMillisecond type which would be to Millisecond what Float64 is to Int?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant