diff --git a/.gitignore b/.gitignore index f15feab..f9c10b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ log/*.log -timelog.csv +log/*.csv diff --git a/pomodoroprompt.py b/pomodoroprompt.py index 0178549..8e7b7da 100644 --- a/pomodoroprompt.py +++ b/pomodoroprompt.py @@ -53,12 +53,7 @@ def quit_prompt(): def log_step(text, utc_start, end_section=False): """ Write detailed log of all events """ - eastern = timezone('US/Eastern') - start = utc_start.astimezone(eastern) - timelog_path = 'log/' + str(start.year) + '-' \ - + format(start.month, '02') + '-' \ - + format(start.day, '02') + '.log' - timelog_file = Path(timelog_path) + timelog_file = Path(timelog_path(ext='log', daily=True)) timelog_file.touch(exist_ok=True) with timelog_file.open('a') as timelog: timelog.write(text + '\n') @@ -71,7 +66,7 @@ def record_task(whatnext, whatdid, start, end=None): """ if end is None: end = datetime.now(pytz.utc) - with open('log/timelog.csv', 'a', newline='') as csvfile: + with open(prepare_file(), 'a', newline='') as csvfile: # TODO make first line started, recorded, description, intention timewriter = csv.writer(csvfile) timewriter.writerow([start, end, whatdid, whatnext]) @@ -89,6 +84,22 @@ def countdown_to(until): print('\r', str_minutes(to_go), sep='', end='') time.sleep(1) +def prepare_file(ext='csv', daily=False, utc_start): + # Ensure log directory exists. + if not os.path.exists('log'): + os.makedirs('log') + # We consider 3am the switchover to a new day, so we skip extra math + # and use Pacific time to get three hours later than our Eastern time. + logpath_tz = timezone('US/Pacific') + logpath_date = utc_start.astimezone(logpath_tz) + timelog_path = 'log/' + str(logpath_date.year); + if (daily): + timelog_path += '-' \ + + format(logpath_date.month, '02') + '-' \ + + format(logpath_date.day, '02') + '.log' + timelog_path += '.' + ext + return timelog_path + def main(): whatdid = '' while True: