From 80dc567a53fd8ad36b054156b580308498a97d25 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 14 Jan 2025 12:45:17 +0100 Subject: [PATCH] Handle negative time in time formatters --- RNS/__init__.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/RNS/__init__.py b/RNS/__init__.py index 68dfe32..d76e170 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -241,6 +241,11 @@ def prettydistance(m, suffix="m"): return "%.2f %s%s" % (num, last_unit, suffix) def prettytime(time, verbose=False, compact=False): + neg = False + if time < 0: + time = abs(time) + neg = True + days = int(time // (24 * 3600)) time = time % (24 * 3600) hours = int(time // 3600) @@ -291,10 +296,17 @@ def prettytime(time, verbose=False, compact=False): if tstr == "": return "0s" else: - return tstr + if not neg: + return tstr + else: + return f"-{tstr}" def prettyshorttime(time, verbose=False, compact=False): + neg = False time = time*1e6 + if time < 0: + time = abs(time) + neg = True seconds = int(time // 1e6); time %= 1e6 milliseconds = int(time // 1e3); time %= 1e3 @@ -338,7 +350,10 @@ def prettyshorttime(time, verbose=False, compact=False): if tstr == "": return "0us" else: - return tstr + if not neg: + return tstr + else: + return f"-{tstr}" def phyparams(): print("Required Physical Layer MTU : "+str(Reticulum.MTU)+" bytes")