forked from hildensia/timecheat
/
timecheat.py
executable file
·158 lines (136 loc) · 6.09 KB
/
timecheat.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python2
import argparse
from random import gauss
from math import sqrt, modf
from datetime import date, datetime, time, timedelta
import calendar
from string import strip
from printer import TextPrinter, LatexPrinter
import locale
from os import environ
def create_times(day, start, pausestart, worktime, pausetime, variance):
mins_hours = modf(start)
start = round_to_quarter(datetime.combine(day, time(int(mins_hours[1]),int(60*mins_hours[0]))) +
timedelta(hours=gauss(0, sqrt(variance))))
pausetime = timedelta(hours=pausetime)
worktime = timedelta(hours=worktime)
pause_mins_hours = modf(pausestart)
pausestart = round_to_quarter(datetime.combine(day, time(int(pause_mins_hours[1]),int(60*mins_hours[0]))) +
timedelta(hours=gauss(0, sqrt(variance))))
pauseend = pausestart + pausetime
end = start + pausetime + worktime
return (start, end, pausestart, pauseend)
def round_to_quarter(time):
minutes_quarter = int(round((time.minute/60.)*4)/4*60)
minutes_ten = int(round(time.minute/10.)*10)
if abs(minutes_quarter-time.minute) < abs(minutes_ten-time.minute):
minutes = minutes_quarter
else:
minutes = minutes_ten
hours = time.hour
if minutes == 60:
minutes = 0
hours += 1
time = time.replace(hour=hours,minute=minutes)
return time
def get_year():
return date.today().year
def get_month():
return date.today().month
def get_holidays(filename):
holiday_file = file(filename)
holidays = []
for h in holiday_file.readlines():
h = strip(h, '\n')
holidays.append(datetime.strptime(h, '%d.%m.%Y').date())
return holidays
def get_work_days(year, month, holiday_file):
cal = calendar.Calendar()
workdays = []
holidays = get_holidays(holiday_file)
for day in cal.itermonthdates(year, month):
if (day.weekday() not in [calendar.SATURDAY, calendar.SUNDAY] and
day not in holidays and
day.month == month):
workdays.append(day)
return workdays
def print_sheet(printer, workdays, args):
printer.print_header(args.month)
printer.print_divider()
(_, week, _) = workdays[0].isocalendar()
day_num_week = 0
day_num_month = 0
for day in workdays:
(s, e, ps, pe) = create_times(day, args.start[0], args.pausestart[0],
args.worktime[0], args.pausetime[0],
args.variance[0])
(_, weeknumber, _) = day.isocalendar()
if weeknumber != week:
printer.print_divider()
printer.print_week(week, day_num_week, args.worktime[0])
printer.print_divider()
day_num_week = 0
week = weeknumber
day_num_week = day_num_week + 1
day_num_month = day_num_month + 1
printer.print_day(s, e, ps, pe, timedelta(hours=args.worktime[0]))
if (day_num_week != 0):
printer.print_divider()
printer.print_week(week, day_num_week, args.worktime[0])
printer.print_divider()
printer.print_month("Januar", day_num_month, args.worktime[0])
printer.print_footer()
def main():
try:
locale.setlocale(locale.LC_TIME, environ['LOCALE'])
except KeyError: # no locale set
pass
parser = argparse.ArgumentParser(description='Create a timesheet with ' +
'gaussian distributed times for work.')
parser.add_argument('--start', nargs=1, metavar='t_s', default=[8],
type=float, help='The time when work normally' +
' starts. Default: 08:00 (=8).')
parser.add_argument('--pausestart', nargs=1, metavar='t_p',
default=[13], type=float, help='Time when the ' +
'lunch break normally starts. Default 13:00 (=13).')
parser.add_argument('--worktime', nargs=1, metavar='t_d', default=[8],
type=float, help='The duration of every work ' +
'day. Default: 8 hours (=8)')
parser.add_argument('--pausetime', nargs=1, metavar='t_pt', default=[.5],
type=float, help='The duration of the lunch ' +
'break. Default: 30 min (=0.5)')
parser.add_argument('--variance', nargs=1, metavar='var', default=[.25],
type=float, help='The variance of each start ' +
'time. Default: 15 min (=.25).')
parser.add_argument('--year', metavar='year', default=get_year(), type=int,
help='The year for which the timesheet'
' should be printed. Default: Current year')
parser.add_argument('--month', metavar='month', default=get_month(),
type=int,
help='The month for which the timesheet'
' should be printed. Default: Current month')
parser.add_argument('--output', nargs=1, metavar='format',
default=['text'], type=str, help='The output format.' +
' May be \'text\', \'template\' or \'latex\'. ' +
'Default: text')
parser.add_argument('--template', nargs=1, metavar='file',
default=['worksheet.tex'], type=str, help='If output' +
' is \'template\' you can specify a template file.')
parser.add_argument('--holidays', nargs=1, metavar='file',
default=['holidays'], type=str, help='A file ' +
'holiday dates. Format is each day in a line in ' +
' german order, e.g.: 24.03.2013')
args = parser.parse_args()
workdays = get_work_days(args.year, args.month, args.holidays[0])
if args.output[0] == 'text':
printer = TextPrinter()
elif args.output[0] == 'template':
printer = LatexPrinter(args.template[0])
elif args.output[0] == 'latex':
printer = LatexPrinter()
else:
parser.print_help()
return
print_sheet(printer, workdays, args)
if __name__ == "__main__":
main()