From 26e52e5bce97fce1405114b833a955819df6b947 Mon Sep 17 00:00:00 2001 From: lara Date: Wed, 15 Apr 2020 11:11:55 -0400 Subject: [PATCH] [work] Fixed parsing for arbitrary pauses --- bin/work | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/bin/work b/bin/work index 351ff8d..c24713c 100755 --- a/bin/work +++ b/bin/work @@ -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