Skip to content

Commit 71cecf7

Browse files
committed
dhcpy.py T6998 - fix datetime to be timezone aware
1 parent fe50656 commit 71cecf7

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/op_mode/dhcp.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def _get_raw_server_leases(family='inet', pool=None, sorted=None, state=[], orig
101101
lifetime = lease['valid-lft']
102102
expiry = (lease['cltt'] + lifetime)
103103

104-
lease['start_timestamp'] = datetime.fromtimestamp(expiry - lifetime, timezone.utc)
104+
lease['start_timestamp'] = datetime.fromtimestamp(lease['cltt'], timezone.utc)
105105
lease['expire_timestamp'] = datetime.fromtimestamp(expiry, timezone.utc) if expiry else None
106106

107107
data_lease = {}
@@ -170,10 +170,8 @@ def _get_formatted_server_leases(raw_data, family='inet'):
170170
ipaddr = lease.get('ip')
171171
hw_addr = lease.get('mac')
172172
state = lease.get('state')
173-
start = lease.get('start')
174-
start = _utc_to_local(start).strftime('%Y/%m/%d %H:%M:%S')
175-
end = lease.get('end')
176-
end = _utc_to_local(end).strftime('%Y/%m/%d %H:%M:%S') if end else '-'
173+
start = datetime.fromtimestamp(lease.get('start'), timezone.utc)
174+
end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-'
177175
remain = lease.get('remaining')
178176
pool = lease.get('pool')
179177
hostname = lease.get('hostname')
@@ -187,10 +185,8 @@ def _get_formatted_server_leases(raw_data, family='inet'):
187185
for lease in raw_data:
188186
ipaddr = lease.get('ip')
189187
state = lease.get('state')
190-
start = lease.get('last_communication')
191-
start = _utc_to_local(start).strftime('%Y/%m/%d %H:%M:%S')
192-
end = lease.get('end')
193-
end = _utc_to_local(end).strftime('%Y/%m/%d %H:%M:%S')
188+
start = datetime.fromtimestamp(lease.get('last_communication'), timezone.utc)
189+
end = datetime.fromtimestamp(lease.get('end'), timezone.utc) if lease.get('end') else '-'
194190
remain = lease.get('remaining')
195191
lease_type = lease.get('type')
196192
pool = lease.get('pool')

0 commit comments

Comments
 (0)