Skip to content

Commit

Permalink
T6998: dhcp.py fix remaining calculating and display
Browse files Browse the repository at this point in the history
  • Loading branch information
metron2 committed Jan 10, 2025
1 parent b67346f commit f216e24
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/op_mode/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand Down

0 comments on commit f216e24

Please sign in to comment.