summaryrefslogtreecommitdiff
path: root/worktime.py
diff options
context:
space:
mode:
Diffstat (limited to 'worktime.py')
-rwxr-xr-xworktime.py53
1 files changed, 31 insertions, 22 deletions
diff --git a/worktime.py b/worktime.py
index 9c72d30..19e2186 100755
--- a/worktime.py
+++ b/worktime.py
@@ -60,7 +60,6 @@ class TogglAPI(object):
60 return now - start if start <= now else None 60 return now - start if start <= now else None
61 61
62class Worktime(object): 62class Worktime(object):
63 time_to_work = timedelta()
64 time_worked = timedelta() 63 time_worked = timedelta()
65 running_entry = None 64 running_entry = None
66 now = datetime.now(tzlocal()) 65 now = datetime.now(tzlocal())
@@ -82,34 +81,40 @@ class Worktime(object):
82 81
83 hours_per_week = float(config.get('WORKTIME', 'HoursPerWeek', fallback=40)) 82 hours_per_week = float(config.get('WORKTIME', 'HoursPerWeek', fallback=40))
84 workdays = set([int(d.strip()) for d in config.get('WORKTIME', 'Workdays', fallback='1,2,3,4,5').split(',')]) 83 workdays = set([int(d.strip()) for d in config.get('WORKTIME', 'Workdays', fallback='1,2,3,4,5').split(',')])
85 hours_per_day = hours_per_week / len(workdays) 84 time_per_day = timedelta(hours = hours_per_week) / len(workdays)
86 85
87 holidays = set() 86 holidays = dict()
88 87
89 for year in range(start_date.year, end_date.year + 1): 88 for year in range(start_date.year, end_date.year + 1):
90 y_easter = datetime.combine(easter(year), time(), tzinfo=tzlocal()) 89 y_easter = datetime.combine(easter(year), time(), tzinfo=tzlocal())
91 90
92 # Legal holidays in munich, bavaria 91 # Legal holidays in munich, bavaria
93 holidays.add(datetime(year, 1, 1, tzinfo=tzlocal()).date()) 92 holidays[datetime(year, 1, 1, tzinfo=tzlocal()).date()] = time_per_day
94 holidays.add(datetime(year, 1, 6, tzinfo=tzlocal()).date()) 93 holidays[datetime(year, 1, 6, tzinfo=tzlocal()).date()] = time_per_day
95 holidays.add((y_easter+timedelta(days=-2)).date()) 94 holidays[(y_easter+timedelta(days=-2)).date()] = time_per_day
96 holidays.add((y_easter+timedelta(days=+1)).date()) 95 holidays[(y_easter+timedelta(days=+1)).date()] = time_per_day
97 holidays.add(datetime(year, 5, 1, tzinfo=tzlocal()).date()) 96 holidays[datetime(year, 5, 1, tzinfo=tzlocal()).date()] = time_per_day
98 holidays.add((y_easter+timedelta(days=+39)).date()) 97 holidays[(y_easter+timedelta(days=+39)).date()] = time_per_day
99 holidays.add((y_easter+timedelta(days=+50)).date()) 98 holidays[(y_easter+timedelta(days=+50)).date()] = time_per_day
100 holidays.add((y_easter+timedelta(days=+60)).date()) 99 holidays[(y_easter+timedelta(days=+60)).date()] = time_per_day
101 holidays.add(datetime(year, 8, 15, tzinfo=tzlocal()).date()) 100 holidays[datetime(year, 8, 15, tzinfo=tzlocal()).date()] = time_per_day
102 holidays.add(datetime(year, 10, 3, tzinfo=tzlocal()).date()) 101 holidays[datetime(year, 10, 3, tzinfo=tzlocal()).date()] = time_per_day
103 holidays.add(datetime(year, 11, 1, tzinfo=tzlocal()).date()) 102 holidays[datetime(year, 11, 1, tzinfo=tzlocal()).date()] = time_per_day
104 holidays.add(datetime(year, 12, 25, tzinfo=tzlocal()).date()) 103 holidays[datetime(year, 12, 25, tzinfo=tzlocal()).date()] = time_per_day
105 holidays.add(datetime(year, 12, 26, tzinfo=tzlocal()).date()) 104 holidays[datetime(year, 12, 26, tzinfo=tzlocal()).date()] = time_per_day
106 105
107 try: 106 try:
108 with open(f"{config_dir}/excused", 'r') as excused: 107 with open(f"{config_dir}/excused", 'r') as excused:
109 for line in excused: 108 for line in excused:
110 stripped_line = line.strip() 109 stripped_line = line.strip()
111 if stripped_line: 110 if stripped_line:
112 holidays.add(datetime.strptime(stripped_line, date_format).replace(tzinfo=tzlocal()).date()) 111 splitLine = stripped_line.split(' ')
112 if len(splitLine) == 2:
113 [hours, date] = splitLine
114 day = datetime.strptime(date, date_format).replace(tzinfo=tzlocal()).date()
115 holidays[day] = timedelta(hours = float(hours))
116 else:
117 holidays[datetime.strptime(stripped_line, date_format).replace(tzinfo=tzlocal()).date()] = time_per_day
113 except IOError as e: 118 except IOError as e:
114 if e.errno != 2: 119 if e.errno != 2:
115 raise e 120 raise e
@@ -130,7 +135,7 @@ class Worktime(object):
130 raise e 135 raise e
131 136
132 137
133 days_to_work = set() 138 days_to_work = dict()
134 139
135 start_day = start_date.date() 140 start_day = start_date.date()
136 end_day = end_date.date() 141 end_day = end_date.date()
@@ -138,14 +143,18 @@ class Worktime(object):
138 end_day = max(end_day, max(list(pull_forward))) 143 end_day = max(end_day, max(list(pull_forward)))
139 144
140 for day in [start_day + timedelta(days = x) for x in range(0, (end_day - start_day).days + 1)]: 145 for day in [start_day + timedelta(days = x) for x in range(0, (end_day - start_day).days + 1)]:
141 if day.isoweekday() in workdays and not day in holidays: 146 if day.isoweekday() in workdays:
142 days_to_work.add(day) 147 time_to_work = time_per_day
148 if day in holidays.keys():
149 time_to_work -= holidays[day]
150 if time_to_work > timedelta():
151 days_to_work[day] = time_to_work
143 152
144 self.is_workday = self.now.date() in days_to_work 153 self.is_workday = self.now.date() in days_to_work
145 154
146 self.time_to_work = timedelta(hours = len([day for day in days_to_work if day <= end_date.date()]) * hours_per_day) 155 self.time_to_work = sum([days_to_work[day] for day in days_to_work.keys() if day <= end_date.date()], timedelta())
147 for day in list(pull_forward): 156 for day in list(pull_forward):
148 days_forward = set([d for d in days_to_work if d >= end_date.date() and d < day and not d in pull_forward]) 157 days_forward = set([d for d in days_to_work.keys() if d >= end_date.date() and d < day and not d in pull_forward])
149 hours_per_day_forward = pull_forward[day] / len(days_forward) if len(days_forward) > 0 else timedelta() 158 hours_per_day_forward = pull_forward[day] / len(days_forward) if len(days_forward) > 0 else timedelta()
150 days_forward.discard(end_date.date()) 159 days_forward.discard(end_date.date())
151 self.time_pulled_forward += pull_forward[day] - hours_per_day_forward * len(days_forward) 160 self.time_pulled_forward += pull_forward[day] - hours_per_day_forward * len(days_forward)