Display hours as a number rather than formatted days
This is useful because the total time is usually not sensibly interpreted as days. Also negatives times were really odd. Includes some autoformating.
This commit is contained in:
parent
a980216fb5
commit
4fe4e3ecbf
33
tx
33
tx
@ -4,38 +4,43 @@ from datetime import date, datetime, timedelta
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
def get_current_date():
|
def get_current_date():
|
||||||
return date.today().strftime("%Y-%m-%d")
|
return date.today().strftime("%Y-%m-%d")
|
||||||
|
|
||||||
|
|
||||||
def get_timefile_line(date, time, desc):
|
def get_timefile_line(date, time, desc):
|
||||||
return date + " " + time + " " + desc + "\n"
|
return date + " " + time + " " + desc + "\n"
|
||||||
|
|
||||||
|
|
||||||
def get_total(filename):
|
def get_total(filename):
|
||||||
with open(filename, 'r') as time_file:
|
with open(filename, 'r') as time_file:
|
||||||
total_time = timedelta()
|
total_time = timedelta()
|
||||||
|
|
||||||
for line in time_file:
|
for line in time_file:
|
||||||
date_time_description = line.split(" ", 2)
|
date_time_description = line.split(" ", 2)
|
||||||
|
|
||||||
hours, minutes = date_time_description[1].split(':', 1)
|
hours, minutes = date_time_description[1].split(':', 1)
|
||||||
|
|
||||||
# Parse the time to integers
|
# Parse the time to integers
|
||||||
hours_int = int(hours)
|
hours_int = int(hours)
|
||||||
minutes_int = int(minutes)
|
minutes_int = int(minutes)
|
||||||
|
|
||||||
# Make sure that both the hours and the minutes are negative if there's a "-" before the time
|
# Make sure that both the hours and the minutes are negative if there's a "-" before the time
|
||||||
if hours_int < 0: minutes_int *= -1
|
if hours_int < 0:
|
||||||
|
minutes_int *= -1
|
||||||
|
|
||||||
delta = timedelta(hours=hours_int, minutes=minutes_int)
|
delta = timedelta(hours=hours_int, minutes=minutes_int)
|
||||||
|
|
||||||
total_time += delta
|
total_time += delta
|
||||||
|
|
||||||
return total_time
|
return "{} hours".format(str(total_time.total_seconds() / 60 / 60))
|
||||||
|
|
||||||
|
|
||||||
# Parse command line arguments
|
# Parse command line arguments
|
||||||
parser = argparse.ArgumentParser(prog='time.txt', description="simple plain-text time tracking.")
|
parser = argparse.ArgumentParser(prog='time.txt', description="simple plain-text time tracking.")
|
||||||
subparsers = parser.add_subparsers(help='Commands', dest='mode')
|
subparsers = parser.add_subparsers(help='Commands', dest='mode')
|
||||||
|
|
||||||
parser.add_argument("-f", "--file", help="a file in the time.txt format. default: ./time.txt")
|
parser.add_argument("-f", "--file", help="a file in the time.txt format. default: ./time.txt")
|
||||||
|
|
||||||
# create the parser for the "a" command
|
# create the parser for the "a" command
|
||||||
@ -43,12 +48,16 @@ parser_total = subparsers.add_parser('total', help='print the total unbilled tim
|
|||||||
|
|
||||||
# create the parser for the "b" command
|
# create the parser for the "b" command
|
||||||
parser_add = subparsers.add_parser('add', help='add a time entry.')
|
parser_add = subparsers.add_parser('add', help='add a time entry.')
|
||||||
parser_add.add_argument("date", nargs='?', default=get_current_date(), help="date in the format 2020-12-30. defaults to the current date.")
|
parser_add.add_argument(
|
||||||
|
"date", nargs='?', default=get_current_date(),
|
||||||
|
help="date in the format 2020-12-30. defaults to the current date.")
|
||||||
parser_add.add_argument("time", help="time in the format 19:20.")
|
parser_add.add_argument("time", help="time in the format 19:20.")
|
||||||
parser_add.add_argument("description", help="short description of how the time was spent.")
|
parser_add.add_argument("description", help="short description of how the time was spent.")
|
||||||
|
|
||||||
parser_bill = subparsers.add_parser('bill', help='add a bill entry (a negative time entry).')
|
parser_bill = subparsers.add_parser('bill', help='add a bill entry (a negative time entry).')
|
||||||
parser_bill.add_argument("date", nargs='?', default=get_current_date(), help="date in the format 2020-12-30. defaults to the current date.")
|
parser_bill.add_argument(
|
||||||
|
"date", nargs='?', default=get_current_date(),
|
||||||
|
help="date in the format 2020-12-30. defaults to the current date.")
|
||||||
parser_bill.add_argument("time", help="time in the format 19:20.")
|
parser_bill.add_argument("time", help="time in the format 19:20.")
|
||||||
parser_bill.add_argument("description", help="short description of how the time was spent.")
|
parser_bill.add_argument("description", help="short description of how the time was spent.")
|
||||||
|
|
||||||
@ -65,14 +74,14 @@ elif args.mode == "add":
|
|||||||
with open(filename, 'a') as time_file:
|
with open(filename, 'a') as time_file:
|
||||||
# TODO: Validate date and time
|
# TODO: Validate date and time
|
||||||
time_file.write(get_timefile_line(args.date, args.time, args.description))
|
time_file.write(get_timefile_line(args.date, args.time, args.description))
|
||||||
|
|
||||||
print("New total:", get_total(filename))
|
print("New total:", get_total(filename))
|
||||||
|
|
||||||
elif args.mode == "bill":
|
elif args.mode == "bill":
|
||||||
with open(filename, 'a') as time_file:
|
with open(filename, 'a') as time_file:
|
||||||
# TODO: Validate date and time
|
# TODO: Validate date and time
|
||||||
time_file.write(get_timefile_line(args.date, "-" + args.time, args.description))
|
time_file.write(get_timefile_line(args.date, "-" + args.time, args.description))
|
||||||
|
|
||||||
print("New total:", get_total(filename))
|
print("New total:", get_total(filename))
|
||||||
|
|
||||||
elif args.mode == "create":
|
elif args.mode == "create":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user