diff options
author | metron2 <metron2@users.noreply.github.com> | 2025-01-06 17:55:13 -0500 |
---|---|---|
committer | Indrajit Raychaudhuri <irc@indrajit.com> | 2025-01-26 12:31:31 -0600 |
commit | 529c24a9d550b30ba7fabcbae3a5bc0d5d35c647 (patch) | |
tree | 4cbb42bec5484f4122875aac02a37697f44adad3 | |
parent | 10ee7ac1d2bfd20bd11b5ebaa33c2f07dd561d48 (diff) | |
download | vyos-1x-529c24a9d550b30ba7fabcbae3a5bc0d5d35c647.tar.gz vyos-1x-529c24a9d550b30ba7fabcbae3a5bc0d5d35c647.zip |
T6998: dhcpy.py - fix datetime to be timezone aware
-rw-r--r-- | python/vyos/kea.py | 12 | ||||
-rwxr-xr-x | src/op_mode/dhcp.py | 13 |
2 files changed, 8 insertions, 17 deletions
diff --git a/python/vyos/kea.py b/python/vyos/kea.py index 951c83693..8b3628db1 100644 --- a/python/vyos/kea.py +++ b/python/vyos/kea.py @@ -476,9 +476,7 @@ def kea_get_server_leases(config, inet, pools=[], state=[], origin=None) -> list lifetime = lease['valid-lft'] expiry = lease['cltt'] + lifetime - lease['start_timestamp'] = datetime.fromtimestamp( - expiry - lifetime, timezone.utc - ) + lease['start_timestamp'] = datetime.fromtimestamp(lease['cltt'], timezone.utc) lease['expire_timestamp'] = ( datetime.fromtimestamp(expiry, timezone.utc) if expiry else None ) @@ -515,14 +513,10 @@ def kea_get_server_leases(config, inet, pools=[], state=[], origin=None) -> list data_lease['remaining'] = '-' if lease['valid-lft'] > 0: - data_lease['remaining'] = lease['expire_timestamp'] - datetime.now( - timezone.utc - ) - - if data_lease['remaining'].days >= 0: + if lease['expire_timestamp'] > datetime.now(timezone.utc): # 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(lease['expire_timestamp'] - datetime.now(timezone.utc)).split('.')[0] # Do not add old leases if ( diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index b3d7d4dd3..7091808e0 100755 --- a/src/op_mode/dhcp.py +++ b/src/op_mode/dhcp.py @@ -19,6 +19,7 @@ import sys import typing from datetime import datetime +from datetime import timezone from glob import glob from ipaddress import ip_address from tabulate import tabulate @@ -110,10 +111,8 @@ def _get_formatted_server_leases(raw_data, family='inet'): ipaddr = lease.get('ip') hw_addr = lease.get('mac') state = lease.get('state') - start = lease.get('start') - start = _utc_to_local(start).strftime('%Y/%m/%d %H:%M:%S') - end = lease.get('end') - end = _utc_to_local(end).strftime('%Y/%m/%d %H:%M:%S') if end else '-' + start = datetime.fromtimestamp(lease.get('start'), timezone.utc) + end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-' remain = lease.get('remaining') pool = lease.get('pool') hostname = lease.get('hostname') @@ -138,10 +137,8 @@ def _get_formatted_server_leases(raw_data, family='inet'): for lease in raw_data: ipaddr = lease.get('ip') state = lease.get('state') - start = lease.get('last_communication') - start = _utc_to_local(start).strftime('%Y/%m/%d %H:%M:%S') - end = lease.get('end') - end = _utc_to_local(end).strftime('%Y/%m/%d %H:%M:%S') + start = datetime.fromtimestamp(lease.get('last_communication'), timezone.utc) + end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-' remain = lease.get('remaining') lease_type = lease.get('type') pool = lease.get('pool') |