Newer
Older
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
import datetime
import uuid
import pytz
date_format = '%Y-%m-%d'
MON, TUE, WED, THU, FRI, SAT, SUN = range(7)
cet = pytz.timezone('Europe/Berlin')
def is_nth_xday_of_month(_date_candidate, _year, _mo, _n, _x):
print(_date_candidate)
print("is " + str(_n) + "th " + str(_x) + "day of " + str(_mo));
_start_string = str(_year) + "-" + str(_mo) + "-01"
if _mo < 10:
_start_string = str(_year) + "-0" + str(_mo) + "-01"
_start = datetime.datetime.strptime(_start_string, date_format)
counter = 0
while _start.month == _mo:
if _start.weekday() == _x:
print("ok, its an " + str(_x) + "day")
counter = counter + 1
print("counter " + str(counter))
if counter == _n:
print("????")
print(_start)
print(_date_candidate)
if _start == _date_candidate:
return True
_start += datetime.timedelta(days=1)
return False
def is_wednesday(_date_candidate):
wd = _date_candidate.weekday()
return wd == WED
def is_nth_monday_of_month(_date_candidate, _n):
return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, MON)
def is_nth_tuesday_of_month(_date_candidate, _n):
return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, TUE)
def is_nth_wednesday_of_month(_date_candidate, _n):
return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, WED)
def is_nth_thursday_of_month(_date_candidate, _n):
return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, THU)
def is_nth_friday_of_month(_date_candidate, _n):
return is_nth_xday_of_month(_date_candidate, _date_candidate.year, _date_candidate.month, _n, FRI)
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
def is_first_monday_of_month(_date_candidate):
is_nth_monday_of_month(_date_candidate, 1)
def is_first_monday_of_month_except_january_an_may_if_monday_is_first(_date_candidate):
_year = _date_candidate.year
if is_january(_date_candidate):
_firstof_string = str(_year) + "-01-01"
_firstof = datetime.datetime.strptime(_firstof_string, date_format)
if _firstof.weekday() == MON:
return is_nth_monday_of_month(_date_candidate, 2)
if is_may(_date_candidate):
_firstof_string = str(_year) + "-05-01"
_firstof = datetime.datetime.strptime(_firstof_string, date_format)
if _firstof.weekday() == MON:
return is_nth_monday_of_month(_date_candidate, 2)
return is_nth_monday_of_month(_date_candidate, 1)
def first_saturday_after_may_10th(_date_candidate):
_year = _date_candidate.year
_start_string = str(_year) + "-05-10"
_start = datetime.datetime.strptime(_start_string, date_format)
while _start.month == 5:
if _start.weekday() == SAT:
if _start == _date_candidate:
return True
else:
return False
_start += datetime.timedelta(days=1)
def last_saturday_in_march(_date_candidate):
_year = _date_candidate.year
_start_string = str(_year) + "-03-20"
_start = datetime.datetime.strptime(_start_string, date_format)
_last_saturday = None
while _start.month == 3:
if _start.weekday() == SAT:
_last_saturday = _start
_start += datetime.timedelta(days=1)
return _date_candidate == _last_saturday
def is_equal_month(_date_candidate):
if _date_candidate.month % 2 == 1:
return True
return False
def is_january(_date_candidate):
return _date_candidate.month == 1
def is_march(_date_candidate):
return _date_candidate.month == 3
def is_april(_date_candidate):
return _date_candidate.month == 4
def is_may(_date_candidate):
return _date_candidate.month == 5
def is_june(_date_candidate):
return _date_candidate.month == 6
def is_july(_date_candidate):
return _date_candidate.month == 7
def is_september(_date_candidate):
return _date_candidate.month == 9
def is_october(_date_candidate):
return _date_candidate.month == 10
def is_november(_date_candidate):
return _date_candidate.month == 11
def is_december(_date_candidate):
return _date_candidate.month == 12
def replace_date_in_template(_template, _tag, _date):
return _template.replace(_tag, _date.strftime("%Y-%m-%d %H:%M:%S %z"))
def set_start_date(_template, _date):
return replace_date_in_template(_template, "<EVENTSTART>", _date)
def set_end_date(_template, _date):
return replace_date_in_template(_template, "<EVENTEND>", _date)
def set_uuid(_template, _uuid):
return _template.replace("<UUID>", str(_uuid))
def set_year(_template, _year):
return _template.replace("<YEAR>", str(_year))
def set_c3number(_template, _year):
return _template.replace("<CCCNumber>", str(_year - 1986))
def modify_template(_template, _start, _end):
_template = set_start_date(_template, _start)
_template = set_end_date(_template, _end)
def create_event(_name, _date, start_hour=00, start_minute=00, end_hour=23, end_minute=59, add_days=0):
with open(_name + ".txt") as f:
_template = f.read()
_start = cet.localize(_date.replace(hour=start_hour, minute=start_minute, second=00))
_end = cet.localize(_date.replace(hour=end_hour, minute=end_minute, second=00))
_end += datetime.timedelta(days=add_days)
_template = modify_template(_template, _start, _end)
with open("created/" + get_filename(_start, _name), 'w') as out:
out.write(_template)