-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzeiten_week.py
More file actions
executable file
·145 lines (122 loc) · 4.36 KB
/
zeiten_week.py
File metadata and controls
executable file
·145 lines (122 loc) · 4.36 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
#!/usr/bin/env python3
import os
import pprint
import re
from datetime import datetime, timedelta
class Element:
IN = 0
OUT = 1
def __init__(self, type, dt):
self.element = {'type': type, 'dt': dt}
def __repr__(self):
return "Element(%d, %s)" % (self.element['type'], self.element['dt'])
def key(self):
return self.element['dt'].date()
def value(self):
return self.element['dt']
def type(self):
return self.element['type']
class Data:
def __init__(self):
self.data = {}
def __repr__(self):
pp = pprint.PrettyPrinter()
return pp.pformat(self.data)
def add(self, e):
"""
Add an Element to the database
"""
key = e.key()
if key in self.data.keys():
elements = self.data[key]
assert elements[-1].type() != e.type(), 'illegal IN and OUT sequence'
self.data[key].append(e)
else:
self.data[key] = []
self.data[key].append(e)
def getDays(self):
"""
Return a sorted list of dates
"""
return sorted(self.data.keys())
def getForDay(self, date):
"""
Returns the sum of a specific day
"""
sum = timedelta(0)
# print "Data::getForDay(", date, "): return ", self.data[date]
if date in self.data.keys():
i_in = None
i_out = None
for i in self.data[date]:
if (i.type() == Element.IN):
i_in = i
elif (i.type() == Element.OUT):
i_out = i
else:
raise ValueError("Illegal type found: ", i.type())
if (i_in != None) and (i_out != None):
sum += i_out.value() - i_in.value()
i_in = None
i_out = None
return sum
def printout(lastMonday, nextMonday, sum):
days, hours, minutes = sum.days, sum.seconds // 3600, sum.seconds // 60 % 60
hours += days * 24
print("From %s to %s -> %02d:%02d (%02.02f) hours" % (lastMonday, nextMonday - timedelta(days=1),
hours, minutes, hours + minutes/60.0))
def main():
FILE_EXT = 'Wissen' + os.sep + 'zeiten'
DB = os.getenv('HOME', 'c:/tmp')
FILE = DB + os.sep + FILE_EXT
data = Data()
# 03/07/2006 9:30:00
pin = re.compile('^IN:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+)')
pout = re.compile('^\s+OUT:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+)')
with open(FILE) as f:
for line in f:
if line.find('#') == 0:
# ignore comments
continue
m_in = pin.match(line)
if (m_in):
# print "IN found: ", m_in.group(1, 2, 3, 4, 5, 6)
d = datetime(int(m_in.group(3)),
int(m_in.group(2)),
int(m_in.group(1)),
int(m_in.group(4)),
int(m_in.group(5)),
int(m_in.group(6)))
# print "d=%s, date=%s" % (d, d.date())
data.add(Element(Element.IN, d))
m_out = pout.match(line)
if (m_out):
# print "OUT found: ", m_out.group(1, 2, 3, 4, 5, 6)
d = datetime(int(m_out.group(3)),
int(m_out.group(2)),
int(m_out.group(1)),
int(m_out.group(4)),
int(m_out.group(5)),
int(m_out.group(6)))
# print "d=", d
data.add(Element(Element.OUT, d))
sum = timedelta(0)
lastMonday = None
nextMonday = None
for d in data.getDays():
if nextMonday and d > nextMonday:
# we have a date after our nextMonday, print out the stuff
printout(lastMonday, nextMonday, sum)
lastMonday = nextMonday
nextMonday = lastMonday + timedelta(days=7)
sum = timedelta(0)
if d.weekday() == 0:
# Monday
if lastMonday:
printout(lastMonday, nextMonday, sum)
lastMonday = d
nextMonday = lastMonday + timedelta(days=7)
sum = timedelta(0)
sum += data.getForDay(d)
if __name__ == '__main__':
main()