1
|
from datetime import timedelta, datetime
|
2
|
|
3
|
__EPOCH = datetime(day=5,month=1,year=1970)
|
4
|
|
5
|
def __date_to_datetime(date):
|
6
|
return datetime(date.year, date.month, date.day)
|
7
|
|
8
|
def weeks_since_epoch(date):
|
9
|
days_since_epoch = (__date_to_datetime(date) - __EPOCH).days
|
10
|
return days_since_epoch // 7
|
11
|
|
12
|
def weekday_ranks(date):
|
13
|
'''Returns n so that if date occurs on a certain weekday, this is the
|
14
|
n-th weekday of the month counting from 0. Also return 4 if this the
|
15
|
last suck weekday of the month. '''
|
16
|
n = 0
|
17
|
month = date.month
|
18
|
i = date - timedelta(days=7)
|
19
|
while i.month == month:
|
20
|
n += 1
|
21
|
i = i - timedelta(days=7)
|
22
|
if (date+timedelta(days=7)).month != month and n != 4:
|
23
|
return n, 4
|
24
|
else:
|
25
|
return n,
|