Skip to content

Commit

Permalink
Fix timezone unaware datetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
lahtinep committed Nov 15, 2024
1 parent 6311b2e commit 0136ca3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion posttroll/address_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, max_age=ten_minutes, port=None,
self._subject = "/address"
self._do_heartbeat = do_heartbeat
self._multicast_enabled = multicast_enabled
self._last_age_check = dt.datetime(1900, 1, 1)
self._last_age_check = dt.datetime(1900, 1, 1, tzinfo=dt.timezone.utc)
self._do_run = False
self._is_running = False
self._thread = threading.Thread(target=self._run)
Expand Down
6 changes: 3 additions & 3 deletions posttroll/ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_configured_nameserver_port():
try:
port = int(os.environ["NAMESERVER_PORT"])
warnings.warn("NAMESERVER_PORT is pending deprecation, please use POSTTROLL_NAMESERVER_PORT instead.",
PendingDeprecationWarning)
PendingDeprecationWarning, stacklevel=2)
except KeyError:
port = DEFAULT_NAMESERVER_PORT
return config.get("nameserver_port", port)
Expand All @@ -68,8 +68,8 @@ def get_pub_addresses(names=None, timeout=10, nameserver="localhost"):
if names is None:
names = ["", ]
for name in names:
then = dt.datetime.now() + dt.timedelta(seconds=timeout)
while dt.datetime.now() < then:
then = dt.datetime.now(dt.timezone.utc) + dt.timedelta(seconds=timeout)
while dt.datetime.now(dt.timezone.utc) < then:
addrs += get_pub_address(name, nameserver=nameserver, timeout=timeout)
if addrs:
break
Expand Down
4 changes: 2 additions & 2 deletions posttroll/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ class _PublisherHeartbeat:
def __init__(self, publisher):
self.publisher = publisher
self.subject = "/heartbeat/" + publisher.name
self.lastbeat = dt.datetime(1900, 1, 1)
self.lastbeat = dt.datetime(1900, 1, 1, tzinfo=dt.timezone.utc)

def __call__(self, min_interval=0):
if not min_interval or (
(dt.datetime.now() - self.lastbeat >=
(dt.datetime.now(dt.timezone.utc) - self.lastbeat >=
dt.timedelta(seconds=min_interval))):
self.lastbeat = dt.datetime.now(dt.timezone.utc)
LOGGER.debug("Publish heartbeat (min_interval is %.1f sec)", min_interval)
Expand Down
4 changes: 2 additions & 2 deletions posttroll/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def start(self):
"""Start the subscriber."""
def _get_addr_loop(service, timeout):
"""Try to get the address of *service* until for *timeout* seconds."""
then = dt.datetime.now() + dt.timedelta(seconds=timeout)
while dt.datetime.now() < then:
then = dt.datetime.now(dt.timezone.utc) + dt.timedelta(seconds=timeout)
while dt.datetime.now(dt.timezone.utc) < then:
addrs = get_pub_address(service, self._timeout, nameserver=self._nameserver)
if addrs:
return [addr["URI"] for addr in addrs]
Expand Down

0 comments on commit 0136ca3

Please sign in to comment.