Skip to content

Commit a91f309

Browse files
committed
Update: Added an option for human readable output for the "subts" command
1 parent 77c979b commit a91f309

4 files changed

Lines changed: 13 additions & 8 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ repos:
6565
- flake8-implicit-str-concat
6666

6767
- repo: https://github.com/pre-commit/mirrors-mypy
68-
rev: v1.17.1
68+
rev: v1.18.2
6969
hooks:
7070
- id: mypy
7171
additional_dependencies: [types-python-dateutil >= 2.9.0.20241206]

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Collection of tools to handle timestamps
66
<img src="https://github.com/sweigmann/time-tools/actions/workflows/python-linux.yml/badge.svg?branch=main">
77
<img src="https://github.com/sweigmann/time-tools/actions/workflows/debian.yml/badge.svg?branch=main">
88
<img src="https://github.com/sweigmann/time-tools/actions/workflows/ubuntu.yml/badge.svg?branch=main">
9-
<img src="https://img.shields.io/github/downloads/sweigmann/time-tools/total">
109

1110
## installation:
1211

src/time_tools/common/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
#
88
# global variables
99
progname = "time-tools"
10-
progver = "0.2"
10+
progver = "0.3"

src/time_tools/ts_to_iso_utc.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# global variables
3131
my_progname = "ts_to_iso_utc"
32-
my_progver = "3"
32+
my_progver = "4"
3333
progname = my_progname + " (" + version.progname + ")"
3434
progver = version.progver + "-" + my_progver
3535
ERR.verbosity = 0
@@ -60,6 +60,7 @@ def parse_args() -> argparse.Namespace:
6060
parser_subts = subparsers.add_parser('subts', help='subtract: timestamp - timestamp -> seconds')
6161
parser_subts.add_argument('ts_younger', type=str, help='timestamp to subtract from')
6262
parser_subts.add_argument('ts_older', type=str, help='subtractor')
63+
parser_subts.add_argument('-r', '--human-readable', action='store_true', help='return hours, minutes and seconds instead of just seconds')
6364
# create the parser for "subsecs"
6465
parser_subsecs = subparsers.add_parser('subsecs', help='subtract: timestamp - seconds -> timestamp')
6566
parser_subsecs.add_argument('ts', type=str, help='timestamp to subtract from')
@@ -86,16 +87,16 @@ def ts_convert(ts: str) -> str:
8687
return isoutctimestamp
8788

8889

89-
def ts_sub_ts(ts1: str, ts2: str) -> str:
90+
def ts_sub_ts(ts1: str, ts2: str) -> float:
9091
datetimeobj1 = parser.parse(ts1, tzinfos=TZM.tzmapping)
9192
datetimeobj2 = parser.parse(ts2, tzinfos=TZM.tzmapping)
9293
if datetimeobj2 > datetimeobj1:
93-
raise ValueError("first timestamp must be older than second timestamp")
94+
raise ValueError("first timestamp must be younger than second timestamp")
9495
utcdatetimeobj1 = datetimeobj1.astimezone(datetime.timezone.utc)
9596
utcdatetimeobj2 = datetimeobj2.astimezone(datetime.timezone.utc)
9697
ts_difference = utcdatetimeobj1 - utcdatetimeobj2
9798
ERR.printmsg(f"Difference: {ts_difference}", ERR.ERRLVL.DEBUG)
98-
return str(ts_difference.total_seconds())
99+
return ts_difference.total_seconds()
99100

100101

101102
def ts_calc_new_ts(ts: str, secs: float, op: str) -> str:
@@ -135,7 +136,12 @@ def __main() -> None:
135136
elif args.command == 'subts':
136137
mytime_o = TSP.tsparse(args.ts_older)
137138
mytime_y = TSP.tsparse(args.ts_younger)
138-
result = ts_sub_ts(mytime_o, mytime_y)
139+
delta = ts_sub_ts(mytime_y, mytime_o)
140+
if args.human_readable:
141+
td = str(datetime.timedelta(seconds=delta)).split(":")
142+
result = f"{td[0]} hours, {int(td[1])} minutes, {int(td[2])} seconds"
143+
else:
144+
result = str(delta)
139145
elif args.command == 'subsecs':
140146
mytime = TSP.tsparse(args.ts)
141147
result = ts_sub_secs(mytime, args.secs)

0 commit comments

Comments
 (0)