diff options
author | Daniil Baturin <daniil@vyos.io> | 2025-01-27 11:50:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-27 11:50:14 +0000 |
commit | cbe364a448c3e65ca1bcdd19c4a8045069853ce3 (patch) | |
tree | 60c5c571d6529c47a82e28835d7807d578aa3225 /src | |
parent | 10ee7ac1d2bfd20bd11b5ebaa33c2f07dd561d48 (diff) | |
parent | e8e05f87e5c23eb59d7895ff50e90f3d3e60e2f1 (diff) | |
download | vyos-1x-cbe364a448c3e65ca1bcdd19c4a8045069853ce3.tar.gz vyos-1x-cbe364a448c3e65ca1bcdd19c4a8045069853ce3.zip |
Merge pull request #4319 from indrajitr/vyos-t6998-retry
dhcp: T6998: Make dhcp lease datetime timezone aware
Diffstat (limited to 'src')
-rwxr-xr-x | src/op_mode/dhcp.py | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/src/op_mode/dhcp.py b/src/op_mode/dhcp.py index b3d7d4dd3..8eed2c6cd 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 @@ -81,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: @@ -110,10 +105,12 @@ 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 +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 = 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') |