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