-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendarUtils.py
76 lines (61 loc) · 2.99 KB
/
CalendarUtils.py
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
"""Functions Relating to Date, Month and Year"""
import calendar
class Calendar(calendar.TextCalendar):
def __init__(self):
calendar.TextCalendar.__init__(self)
self.datetime = calendar.datetime.datetime
self.timedelta = calendar.datetime.timedelta
Inityear = self.datetime.now().year
Initmonth = self.datetime.now().month
self.date = self.datetime(Inityear, Initmonth, 1)
self.today = self.datetime.now().day
#Calendar Widget
def getHeader(self):
return self.formatmonthname(self.date.year, self.date.month, 0)
def getMonthDays(self):
monthdays = self.monthdayscalendar(self.date.year, self.date.month)
return [day for week in monthdays for day in week]
def set_prevMonth(self):
self.date = self.date - self.timedelta(days=1)
self.date = self.datetime(self.date.year, self.date.month, 1)
def set_nextMonth(self):
self.date = self.date + self.timedelta(days=calendar.monthrange(self.date.year, self.date.month)[1] + 1)
self.date = self.datetime(self.date.year, self.date.month, 1)
def set_MonthYear(self, year, month):
self.date = self.datetime(year, month, 1)
def isDateReal(self, year, month, day): #validate date
try:
self.datetime(year, month, day)
except ValueError:
return False
else:
return True
def sqlDateFormat(self, year, month, date): #change date format to integer for db storage
return year*10000 + month*100 + date
#Weekly Stats
def getWeekDay(self, year, month, day): #returns the weekday of the given date
return calendar.weekday(year, month, day)
def createSQLDatesWeek(self, year, month, day): #returns the week starting from given date
datelist = []
date = self.datetime(year, month, day)
for x in range(0,7):
datelist.append(self.sqlDateFormat(date.year, date.month, date.day))
date = date + self.timedelta(days=1)
date = self.datetime(date.year, date.month, date.day)
return datelist
#Monthly Stats
def convertMonthNametoNumber(self, monthname): #converts month names to their month number
monthnames = list(calendar.month_name)
return monthnames.index(monthname)
def getDaysinMonth(self, year, month): #returns number of days in the given month
return calendar.monthrange(year, month)[1]
def sqlDateUnpack(self, sqldate): #convert integer date into component date
year, remainder = divmod(sqldate, 10000)
month, remainder = divmod(remainder, 100)
date = remainder
return year, month, date
def createSQLDatesMonth(self, year, month): #returns the month starting from given date
datelist = []
for d in range(0, self.getDaysinMonth(year, month)):
datelist.append(self.sqlDateFormat(year, month, d+1))
return datelist