37 lines
1.0 KiB
Plaintext
Raw Normal View History

2020-12-14 23:50:16 +01:00
#!/usr/bin/env python3
from datetime import datetime, timedelta
import argparse
# Parse command line arguments
parser = argparse.ArgumentParser(description="Description")
# add arguments to the parser
parser.add_argument("-f", "--file")
2020-12-15 00:34:52 +01:00
parser.add_argument("mode",
help="display the square of a given number",
nargs='?', default="total",
2020-12-15 20:11:40 +01:00
choices=["total", "add"])
args = parser.parse_args()
2020-12-15 20:11:40 +01:00
filename = args.file if args.file else 'time.txt'
2020-12-14 23:50:16 +01:00
2020-12-15 00:34:52 +01:00
if args.mode == "total":
2020-12-15 20:11:40 +01:00
with open(filename, 'r') as time_file:
total_time = timedelta()
2020-12-14 23:50:16 +01:00
2020-12-15 20:11:40 +01:00
for line in time_file:
date_time_description = line.split(" ", 2)
hours, minutes = date_time_description[1].split(':', 1)
delta = timedelta(hours=int(hours), minutes=int(minutes))
total_time += delta
2020-12-14 23:50:16 +01:00
2020-12-15 20:11:40 +01:00
print('Total time: ', total_time)
elif args.mode == "add":
with open(filename, 'a') as time_file:
time_file.write("A new line")