Skip to content

Commit

Permalink
Remove timezone information when querying local time
Browse files Browse the repository at this point in the history
datetime.now(timezone.utc) includes tzinfo whereas datetime.utcnow()
(which is now deprecated so it was changed to the former) does not
include tzinfo. This is a simple fix to just remove the tzinfo as there
is no timezone info for a flight's departure time (when comparing a time
with and without tzinfo, an exception would be thrown).

See #256 (comment)
for a traceback of the issue before this fix.
  • Loading branch information
jdholtz committed May 15, 2024
1 parent 8f3978c commit de9bc3d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_current_time() -> datetime:
response = c.request(NTP_SERVER, version=3, timeout=10)
except (socket.gaierror, ntplib.NTPException):
logger.debug("Error requesting time from NTP server. Using local time")
return datetime.now(timezone.utc)
return datetime.now(timezone.utc).replace(tzinfo=None)

return datetime.fromtimestamp(response.tx_time, timezone.utc).replace(tzinfo=None)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import socket
from datetime import datetime
from datetime import datetime, timezone
from typing import Any

import ntplib
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_get_current_time_returns_local_datetime_on_failed_request(
) -> None:
mocker.patch("ntplib.NTPClient.request", side_effect=exception)
mock_datetime = mocker.patch("lib.utils.datetime")
mock_datetime.now.return_value = datetime(1999, 12, 31, 18, 59, 59)
mock_datetime.now.return_value = datetime(1999, 12, 31, 18, 59, 59, tzinfo=timezone.utc)

assert utils.get_current_time() == datetime(1999, 12, 31, 18, 59, 59)

Expand Down

0 comments on commit de9bc3d

Please sign in to comment.