- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateutils.py
More file actions
Latest commit
149 lines (109 loc) · 4.89 KB
/
Copy pathdateutils.py
File metadata and controls
149 lines (109 loc) · 4.89 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
fromdatetimeimportdate, timedelta
defgenerate_date_list(start_date, end_date):
"""Create a range of dates and return it as a list of strings"""
asserttype(start_date) isdate
asserttype(end_date) isdate
tDelta=end_date-start_date
return [start_date+timedelta(days=i) foriinrange(tDelta.days+1)]
defget_next_days(start_date, days_forward):
asserttype(start_date) isdate
asserttype(days_forward) isint
returngenerate_date_list(start_date, start_date+timedelta(days=days_forward))
defdates_for_weekday(dates, day):
"""Filters the list of dates by a weekday specified (0-6).
0 is Monday, 6 is Sunday"""
asserttype(day) isint
return [datefordateindatesifdate.weekday() ==day]
###
### Abstractions on top of dates_for_weekday to make code more readable
### Note: these do not currently have tests
###
defget_all_mondays(dates):
returndates_for_weekday(dates, 0)
defget_all_tuesdays(dates):
returndates_for_weekday(dates, 1)
defget_all_wednesdays(dates):
returndates_for_weekday(dates, 2)
defget_all_thursdays(dates):
returndates_for_weekday(dates, 3)
defget_all_fridays(dates):
returndates_for_weekday(dates, 4)
defget_all_saturdays(dates):
returndates_for_weekday(dates, 5)
defget_all_sundays(dates):
returndates_for_weekday(dates, 6)
defdate_of_upcoming_weekday(given_date, weekday):
"""Returns the date of the weekday closest to given_date where weekday is
an int 0-6 where 0 represents Monday, and 6 represents Sunday"""
asserttype(given_date) isdate
asserttype(weekday) isint
returngiven_date+timedelta(days=days_to_weekday(given_date, weekday))
### Abstractions on top of date_of_upcoming_weekday to make code more readable
### Note: These do not currently have tests
defdate_of_upcoming_monday(given_date):
returndate_of_upcoming_weekday(given_date, 0)
defdate_of_upcoming_tuesday(given_date):
returndate_of_upcoming_weekday(given_date, 1)
defdate_of_upcoming_wednesday(given_date):
returndate_of_upcoming_weekday(given_date, 2)
defdate_of_upcoming_thursday(given_date):
returndate_of_upcoming_weekday(given_date, 3)
defdate_of_upcoming_friday(given_date):
returndate_of_upcoming_weekday(given_date, 4)
defdate_of_upcoming_saturday(given_date):
returndate_of_upcoming_weekday(given_date, 5)
defdate_of_upcoming_sunday(given_date):
returndate_of_upcoming_weekday(given_date, 6)
defdays_to_weekday(given_date, target_weekday):
"""Returns the ammount of days from the given_date to the target_weekday.
target_weekday is an int 0-6, where 0 represents Monday, and 6 represents Sunday"""
asserttype(given_date) isdate
asserttype(target_weekday) isint
return_forward_steps_to_target(target_weekday, (0,1,2,3,4,5,6), given_date.weekday())
### Abstractions on top of days_to_weekday to make code more readable
### Note: These do not currently have tests
defdays_until_monday(given_date):
returndays_to_weekday(given_date, 0)
defdays_until_tuesday(given_date):
returndays_to_weekday(given_date, 1)
defdays_until_wednesday(given_date):
returndays_to_weekday(given_date, 2)
defdays_until_thursday(given_date):
returndays_to_weekday(given_date, 3)
defdays_until_friday(given_date):
returndays_to_weekday(given_date, 4)
defdays_until_saturday(given_date):
returndays_to_weekday(given_date, 5)
defdays_until_sunday(given_date):
returndays_to_weekday(given_date, 6)
def_forward_steps_to_target(needle, haystack, start=0):
"""A needle to search for in a SORTED HAYSTACK OF INTEGERS. It starts the search at the index
of start. It will return the amount of steps forward from the start of the search
to the end. If the needle is before the start location, the returned value will be the amount
of steps foward if the haystack were duplicated. If the needle is at the start position, the
function will still search forward. If the needle is not in the haystack, returns -1"""
asserttype(needle) isint
asserttype(haystack) istuple
asserttype(start) isint
assertneedleinhaystack
dblStack=haystack*2
abbrStack=dblStack[start:]
steps=0
found=False
ifneedlenotinhaystack:
steps=-1
else:
whilesteps<len(abbrStack) andnotfound:
steps+=1
ifabbrStack[steps] ==needle:
found=True
returnsteps
defdates_in_week(start_date):
"""Returns a list of 7 dates from start_date, to 6 days in the future"""
asserttype(start_date) isdate
returngenerate_date_list(start_date, start_date+timedelta(weeks=1))
defdates_from_start_date(start_date, days_after_start):
"""Generates a list of dates with from start_date to the amount of days after the start"""
asserttype(start_date) isdate
asserttype(days_after_start) isint
returngenerate_date_list(start_date, start_date+timedelta(days=days_after_start))