From 529c24a9d550b30ba7fabcbae3a5bc0d5d35c647 Mon Sep 17 00:00:00 2001 From: metron2 Date: Mon, 6 Jan 2025 17:55:13 -0500 Subject: T6998: dhcpy.py - fix datetime to be timezone aware --- src/op_mode/dhcp.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src') 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') -- cgit v1.2.3 From e8e05f87e5c23eb59d7895ff50e90f3d3e60e2f1 Mon Sep 17 00:00:00 2001 From: Indrajit Raychaudhuri Date: Wed, 22 Jan 2025 01:42:33 -0600 Subject: T6998: Remove vestigial helper and reformat --- python/vyos/kea.py | 23 ++++++++++++----------- src/op_mode/dhcp.py | 22 +++++++++++++--------- 2 files changed, 25 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/python/vyos/kea.py b/python/vyos/kea.py index 8b3628db1..baac75eda 100644 --- a/python/vyos/kea.py +++ b/python/vyos/kea.py @@ -474,10 +474,11 @@ def kea_get_server_leases(config, inet, pools=[], state=[], origin=None) -> list data = [] for lease in leases: lifetime = lease['valid-lft'] - expiry = lease['cltt'] + lifetime + start = lease['cltt'] + expiry = start + lifetime - lease['start_timestamp'] = datetime.fromtimestamp(lease['cltt'], timezone.utc) - lease['expire_timestamp'] = ( + lease['start_time'] = datetime.fromtimestamp(start, timezone.utc) + lease['expire_time'] = ( datetime.fromtimestamp(expiry, timezone.utc) if expiry else None ) @@ -491,7 +492,7 @@ def kea_get_server_leases(config, inet, pools=[], state=[], origin=None) -> list else '-' ) data_lease['end'] = ( - lease['expire_timestamp'].timestamp() if lease['expire_timestamp'] else None + lease['expire_time'].timestamp() if lease['expire_time'] else None ) data_lease['origin'] = 'local' # TODO: Determine remote in HA # remove trailing dot in 'hostname' to ensure consistency for `vyos-hostsd-client` @@ -499,10 +500,10 @@ def kea_get_server_leases(config, inet, pools=[], state=[], origin=None) -> list if inet == '4': data_lease['mac'] = lease['hw-address'] - data_lease['start'] = lease['start_timestamp'].timestamp() + data_lease['start'] = lease['start_time'].timestamp() if inet == '6': - data_lease['last_communication'] = lease['start_timestamp'].timestamp() + data_lease['last_communication'] = lease['start_time'].timestamp() data_lease['duid'] = _format_hex_string(lease['duid']) data_lease['type'] = lease['type'] @@ -512,11 +513,11 @@ def kea_get_server_leases(config, inet, pools=[], state=[], origin=None) -> list data_lease['remaining'] = '-' - if lease['valid-lft'] > 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(lease['expire_timestamp'] - datetime.now(timezone.utc)).split('.')[0] + now = datetime.now(timezone.utc) + if lease['valid-lft'] > 0 and lease['expire_time'] > now: + # 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(lease['expire_time'] - now).split('.')[0] # Do not add old leases if ( diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index 7091808e0..8eed2c6cd 100755 --- a/src/op_mode/dhcp.py +++ b/src/op_mode/dhcp.py @@ -82,12 +82,6 @@ ArgState = typing.Literal[ ArgOrigin = typing.Literal['local', 'remote'] -def _utc_to_local(utc_dt): - return datetime.fromtimestamp( - (datetime.fromtimestamp(utc_dt) - datetime(1970, 1, 1)).total_seconds() - ) - - def _get_raw_server_leases( config, family='inet', pool=None, sorted=None, state=[], origin=None ) -> list: @@ -112,7 +106,11 @@ def _get_formatted_server_leases(raw_data, family='inet'): hw_addr = lease.get('mac') state = lease.get('state') start = datetime.fromtimestamp(lease.get('start'), timezone.utc) - end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-' + 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') @@ -137,8 +135,14 @@ def _get_formatted_server_leases(raw_data, family='inet'): for lease in raw_data: ipaddr = lease.get('ip') state = lease.get('state') - start = datetime.fromtimestamp(lease.get('last_communication'), timezone.utc) - end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-' + 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') -- cgit v1.2.3