[work] Fixed parsing for arbitrary pauses

This commit is contained in:
lara 2020-04-15 11:11:55 -04:00
parent a8c10982fb
commit 26e52e5bce

View file

@ -164,9 +164,9 @@ def work_end(args):
if not fields:
die("Why try to leave when you haven't even started")
fields.append(hour)
# Test for even number of timestamp (but fields[0] is the date)
if len(fields) < 3:
die("not enough fields in {}".format(TODAY_FILE))
# Test for even number of timestamp (but fields[0] is the date)
elif len(fields) % 2 == 0:
die("odd number of timestamps in {}".format(TODAY_FILE))
begin_time = None
@ -215,20 +215,33 @@ def work_parse(args):
if len(fields) < 6:
log("Record: '{}' hasn't got enough fields ({})".format(line, len(fields)))
continue
morning = datetime.strptime(fields[1], "%H:%M")
break_start = datetime.strptime(fields[2], "%H:%M")
break_end = datetime.strptime(fields[3], "%H:%M")
evening = datetime.strptime(fields[4], "%H:%M")
break_time = break_end - break_start
times = []
for field in fields[1:]:
try:
time = datetime.strptime(field, "%H:%M")
times.append(time)
except ValueError:
break
if len(times) % 2 != 0:
die("Error: uneven number of timestamps ({}) in line '{}'".format(len(times), line))
desc = ','.join(fields[len(times)+1:]).strip()
worked_time = timedelta()
for i in range(int(len(times) / 2)):
worked_time += times[i * 2 + 1] - times[i * 2]
morning = times[0]
evening = times[-1]
full_day = evening - morning
work_day = full_day - break_time
break_time = full_day - worked_time
new_lines.append('{},{},{},{},{},{}'.format(
fields[0],
morning.strftime("%H:%M"),
evening.strftime("%H:%M"),
td_format(break_time),
td_format(work_day),
"".join(fields[5:]).strip()))
td_format(worked_time),
desc))
except FileNotFoundError:
die("file not found: {}".format(args.file))
# TODO do sanity checking, like if a day already exists