9
def read_timelog(filename):
12
def todays_entries(today, lines):
13
# assume "day turnover" at 2 am
14
min = datetime.datetime.combine(today, datetime.time(2, 0))
15
max = min + datetime.timedelta(1)
17
time = line.split(': ', 1)[0]
19
time = parse_datetime(time)
27
m = re.match(r'^(\d+)-(\d+)-(\d+)$', dt)
29
raise ValueError('bad date: ', dt)
30
year, month, day = map(int, m.groups())
31
return datetime.date(year, month, day)
33
def parse_datetime(dt):
34
m = re.match(r'^(\d+)-(\d+)-(\d+) (\d+):(\d+)$', dt)
36
raise ValueError('bad date time: ', dt)
37
year, month, day, hour, min = map(int, m.groups())
38
return datetime.datetime(year, month, day, hour, min)
40
def calculate_diffs(lines):
43
time, action = line.split(': ', 1)
44
time = parse_datetime(time)
48
delta = time - last_time
49
yield last_time, time, delta, action.strip()
55
return '%d hour%s %d min' % (h, h != 1 and "s" or "", m)
57
return '%d hour%s' % (h, h != 1 and "s" or "")
61
def print_diff(last_time, time, delta, action):
62
time = time.strftime('%H:%M')
66
delta = format_time(delta.seconds / 60)
69
## print "%s%15s %s" % (time, delta, action)
72
action = action[:1].title() + action[1:]
74
print "%s at %s\n" % (action, time)
76
print "%-62s %s" % (action, delta)
78
def print_diffs(iter):
81
total_time = total_slack = datetime.timedelta(0)
82
for last_time, time, delta, action in iter:
83
if first_time is None:
85
print_diff(last_time, time, delta, action)
91
return first_time, time, total_time, total_slack
94
def main(argv=sys.argv):
95
filename = 'timelog.txt'
96
opts, args = getopt.getopt(argv[1:], 'hf:', ['help'])
101
print >> sys.stderr, "too many arguments"
103
if args[0] == 'yesterday':
104
today = datetime.date.today() - datetime.timedelta(1)
106
today = parse_date(args[0])
108
if os.path.basename(argv[0]).replace('.py', '') == 'yesterday':
109
today = datetime.date.today() - datetime.timedelta(1)
111
today = datetime.date.today()
113
title = "Today, %s" % today.strftime('%Y-%m-%d')
115
print "-" * len(title)
116
chain = read_timelog(filename)
117
chain = todays_entries(today, chain)
118
chain = calculate_diffs(chain)
119
first_time, last_time, total_time, total_slack = print_diffs(chain)
121
now = datetime.datetime.now()
123
print "Total work done: %s" % format_time(total_time.seconds / 60)
124
print "Time spent slacking: %s" % format_time(total_slack.seconds / 60)
126
print "Time now: %s" % now.strftime('%H:%M')
127
if last_time is not None:
128
delta = now - last_time
129
print "Time since last entry: %s" % format_time(delta.seconds / 60)
130
delta = now - first_time
131
print "Time since first entry: %s" % format_time(delta.seconds / 60)
132
est_end_of_work = last_time + datetime.timedelta(hours=8) - total_time
133
delta = est_end_of_work - now
134
print "Time left at work: %s (til %s)" % (
135
format_time(delta.seconds / 60),
136
est_end_of_work.strftime("%H:%M"))
139
if __name__ == '__main__':