-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.txt
46 lines (37 loc) · 1.03 KB
/
notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
daily:X
weekly:X:XXXXXXX
monthly:X:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
monthly:X:XXXXX:XXXXXXX
yearly:X:XXXXXXXXXXXX
yearly:X:XXXXXXXXXXXX(:XXXXX:XXXXXXX)
dates = calculate_recurrence(Date.today,'weekly','2:91')
def check(dates)
dates.each { |date| p "#{date.strftime('%A %B %d, %Y')}" };nil
end
# Binary representation of days of the week
0 1 Saturday
1 2 Friday
2 3 Thursday
4 4 Wednesday
8 5 Tuesday
16 6 Monday
32 7 Sunday
require 'date'
class Date
def nth_weekday(nth,day_of_week)
nth = nth + 1
last = nth == 6
nth = 5 if last
date = Date.civil(self.year,self.month)
current_week_day = date.cwday
day_of_week = 7 if day_of_week == 0
nth -= 1 if day_of_week >= current_week_day
offset = (7*nth - (current_week_day - day_of_week))
new_date = date + (7*nth - (current_week_day - day_of_week))
return new_date if new_date.month == date.month
return last ? new_date - 7 : nil
end
end
puts Date.today.nth_weekday(3,3).to_s
puts Date.today.nth_weekday(4,3).to_s
puts Date.today.nth_weekday(6,3).to_s