summaryrefslogtreecommitdiff
path: root/tropical/calstate.py
blob: 7bf285a40beb4ef6000a5bf02215af8e17393b10 (plain)
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

import calendar
from datetime import date, timedelta, datetime

class CalState:

    def __init__(self):
        self.gotoToday()
        self.firstWeekDay=0

    def setFirstWeekDay(self, i):
        self.firstWeekDay=i
        
    def goto(self, year, month, day):
        self.year=year
        self.month=month
        self.day=day

    def gotoToday(self):
        today = date.today()
        self.goto(today.year, today.month, today.day)

    def today(self):
        t=datetime.today()
        return (t.year,t.month,t.day,int(t.strftime("%w"))-1)
        
    def gotoNextWeek(self):
        day=date(self.year,self.month,self.day) + timedelta(weeks=1)
        self.goto(day.year,day.month, day.day)

    def gotoPrevWeek(self):
        day=date(self.year,self.month,self.day) - timedelta(weeks=1)
        self.goto(day.year,day.month, day.day)
        
    def getMonthDays(self):
        cal=calendar.Calendar()
        days=list()
        for day in cal.itermonthdays4(self.year, self.month):
            days.append(day)
        return days

    def getWeekDays(self):
        daysMonth=self.getMonthDays()
        daysWeek=list()
        for year, month, day, count in daysMonth:
            daysWeek.append((year,month,day,count))
            if len(daysWeek) >= 7:
                # Check if day within the week
                for yy, mm, dd, cc in daysWeek:
                    if (yy, mm, dd) == (self.year, self.month, self.day):
                        return daysWeek
                daysWeek.clear()
        return None