Last active
June 2, 2017 06:22
-
-
Save havran/1f071e4373d4401a3700b00e0229e98e to your computer and use it in GitHub Desktop.
Compute next day in two weeks interval from start_date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compute next day in two weeks interval from start_date. | |
# date - current day (today) | |
# start_date - date from which interval started | |
# day_of_week - day of week as number monday = 1 ... sunday = 0 | |
def get_next_2week_day(date, start_date, day_of_week) | |
date = date.to_date | |
next_day = date + ((day_of_week - date.wday) % 7) | |
next_next_day = next_day + 7.days | |
start_date = start_date.to_date | |
weeks = ((date - start_date.to_date)/7).to_i | |
week_multiplier = (weeks % 2) == 0 | |
# puts "#{date} #{start_date} #{week_multiplier} #{next_day} #{next_next_day}" | |
if date >= start_date | |
if week_multiplier | |
date.wday == day_of_week ? date : next_next_day | |
else | |
date.wday == day_of_week ? next_next_day : next_day | |
end | |
else | |
# ??? | |
end | |
end | |
get_next_2week_day(Date.new(2016,2,5),Date.new(2016,2,4),4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment