bunkat / later

A javascript library for defining recurring schedules and calculating future (or past) occurrences for them. Includes support for using English phrases and Cron schedules. Works in Node and in the browser.
http://bunkat.github.io/later/
MIT License
2.42k stars 246 forks source link

How to get N-th occurence? #164

Open boenrobot opened 8 years ago

boenrobot commented 8 years ago

I have a schedule, and I want to add info about specific occurrences of the schedule. I'm hoping to use Later to find out the occurrence times out of the schedule times.

While doing sched.next(5) would work for getting the first 5 occurrences of the schedule, many of the tasks in the schedule are going to repeat for a long time, meaning that it may eventually reach in the hundreds of thousands, making this a no go. There's also no telling when is which occurrence going to be modified, making the "start" and "end" arguments insufficient too.

Is there a way to get f.e. the 5th occurrence of a schedule, without ending up with all previous occurrences in memory? If there isn't one, consider this a feature request.

I guess I could "manually" do this by getting the next 1 occurrence, and use it as the "start" of another call, and do a total of N calls, getting the last occurrence as the one... But surely, doing that would become slow over time (as N grows and more calls are necessary), which is also something to be avoided.

bunkat commented 8 years ago

There is no way to calculate the nth occurrence without calculating the previous nth-1 occurrence. I'm not sure I understand the scenario where you would need to find the 100,000th occurrence of a schedule. Also, 100,000th occurrence from when?

boenrobot commented 8 years ago

In my app, each task has a start date and time (of the first occurrence), a duration, and a recurrence rule. The scenario is attaching notes to a particular occurrence of recurring tasks, which may happen before or after the fact.

I take it in Later's terms, the recurrence rule is the schedule, and the start time is the one given as the start of the first next()/prev() call?

Hmm... considering that people will usually add notes to recent occurrences (last few, or upcoming few), I guess I could base the start on the current time, and get the N-th next()/prev() one (which would be far more efficient), but then the question becomes how to "align" that with the start date and time of the task's first occurrence.