-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateexample.py
More file actions
53 lines (38 loc) · 1.41 KB
/
dateexample.py
File metadata and controls
53 lines (38 loc) · 1.41 KB
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
47
48
49
50
51
52
53
from datetime import date, datetime, timedelta
from datetime import timedelta
def week_range(date):
"""Find the first/last day of the week for the given day.
Assuming weeks start on Sunday and end on Saturday.
Returns a tuple of ``(start_date, end_date)``.
"""
# isocalendar calculates the year, week of the year, and day of the week.
# dow is Mon = 1, Sat = 6, Sun = 7
year, week, dow = date.isocalendar()
# Find the first day of the week.
if dow == 7:
# Since we want to start with Sunday, let's test for that condition.
start_date = date
else:
# Otherwise, subtract `dow` number days to get the first day
start_date = date - timedelta(dow)
# Now, add 6 for the last day of the week (i.e., count up to Saturday)
end_date = start_date + timedelta(6)
return (start_date, end_date)
def datespan(startDate, endDate, delta=timedelta(days=7)):
currentDate = startDate
while currentDate < endDate:
yield currentDate
currentDate += delta
# for day in datespan(date(2007, 3, 30), date(2007, 5, 3),
# delta=timedelta(days=7)):
# print day
# get week range based on date
d = datetime(2013, 3, 7)
print week_range(d)
# based on ist calender
def foo(year, week):
d = date(year,1,1)
d = d - timedelta(d.weekday())
dlt = timedelta(days = (week-1)*7)
return d + dlt, d + dlt + timedelta(days=6)
print foo(2016, 3)