From f216e24e8dd60d39136e28a08abfbc0d17b993eb Mon Sep 17 00:00:00 2001
From: metron2 <metron2@users.noreply.github.com>
Date: Thu, 9 Jan 2025 23:59:08 -0500
Subject: [PATCH] T6998: dhcp.py fix remaining calculating and display

---
 src/op_mode/dhcp.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py
index 72c0a2d0f0..37387dea2c 100755
--- a/src/op_mode/dhcp.py
+++ b/src/op_mode/dhcp.py
@@ -18,7 +18,7 @@
 import sys
 import typing
 
-from datetime import datetime
+from datetime import datetime, timedelta
 from datetime import timezone
 from glob import glob
 from ipaddress import ip_address
@@ -132,12 +132,14 @@ def _get_raw_server_leases(family='inet', pool=None, sorted=None, state=[], orig
         data_lease['remaining'] = '-'
 
         if lease['valid-lft'] > 0:
-            data_lease['remaining'] = lease['expire_timestamp'] - datetime.now(timezone.utc)
+            # Calculate the seconds remaining in the lease
+            remaining = expiry - datetime.now(timezone.utc).timestamp()
 
-            if data_lease['remaining'].days >= 0:
+            # If there is time remaining, display it
+            if remaining > 0:
                 # substraction gives us a timedelta object which can't be formatted with strftime
                 # so we use str(), split gets rid of the microseconds
-                data_lease['remaining'] = str(data_lease['remaining']).split('.')[0]
+                data_lease['remaining'] = str(timedelta(seconds=remaining)).split('.')[0]
 
         # Do not add old leases
         if data_lease['remaining'] != '' and data_lease['pool'] in pool and data_lease['state'] != 'free':