Bloomberg
Bloomberg

Reputation: 2367

how can i get date of every 7th day from start date for a period of time

how can i get date of every 7th day from start date for a period of time.

I have a scheduler that is supposed to run at every 7 days after its start datetime is set to send push notification for every time zone.

eg.

   start_time              run_every          end_time
20-july-2016 10:00:00      7_days       25-dec-2016 10:00:00

then i will run scheduler every 7 days from 20-july-2016 at 10 am. that will be 28-july, 4-aug, 11-aug and so on.

The problem is its supposed to be run for all time zones so that user in australia can get notification on 20-july-2016 10:00:00 and user in US should also get notification on 20-july-2016 10:00:00.

for that I am running scheduler on start_date, start_date+1.days, start_date-1.days because my server is in UTC timezone so that it can occupy 10 am of every timezone.

I cant figure out a way to run it in intervals of 7 days. how can i get next running dates and check every day that should i run this notification today?

Upvotes: 3

Views: 337

Answers (2)

Nic Nilov
Nic Nilov

Reputation: 5155

To get the list of running dates in UTC you could use this:

(1.month.ago.utc.to_i..1.week.from_now.utc.to_i).
  step(7.days).
  map { |v| Time.at(v).utc }

# => [2016-06-18 17:27:18 UTC, 2016-06-25 17:27:18 UTC, 2016-07-02 17:27:18 UTC, 2016-07-09 17:27:18 UTC, 2016-07-16 17:27:18 UTC, 2016-07-23 17:27:18 UTC]

From this point you should be able to perform any timezone adjustments you like. Note that Range has timezone-aware methods such as step_with_time_with_zone.

Also you might want to consider using whenever gem for cron scheduling (if you use that) or ice_cube gem for recurrence calculation.

Upvotes: 4

Vucko
Vucko

Reputation: 20844

I had a similar task and I used ice_cube gem, which makes "recurring events" easier to handle.

Daily example:

# every third day
schedule = Schedule.new(Time.now)
schedule.add_recurrence_rule IceCube::Rule.daily(3)

# Occurrences until a time
schedule.occurrences Time.tomorrow

Upvotes: 3

Related Questions