Skip to content

Commit

Permalink
Merge pull request #69 from pnuu/feature-timezone-aware-datetimes
Browse files Browse the repository at this point in the history
Switch to timezone aware datetimes
  • Loading branch information
mraspaud authored Jan 27, 2025
2 parents 7b2c565 + 02fd7ec commit 77f8410
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 160 deletions.
26 changes: 0 additions & 26 deletions posttroll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

"""Posttroll packages."""

import datetime as dt
import logging
import sys

from donfig import Config

Expand All @@ -46,27 +44,3 @@ def get_context():
return get_context()
else:
raise NotImplementedError(f"No support for backend {backend} implemented (yet?).")


def strp_isoformat(strg):
"""Decode an ISO formatted string to a datetime object.
Allow a time-string without microseconds.
We handle input like: 2011-11-14T12:51:25.123456
"""
if isinstance(strg, dt.datetime):
return strg
if len(strg) < 19 or len(strg) > 26:
if len(strg) > 30:
strg = strg[:30] + "..."
raise ValueError("Invalid ISO formatted time string '%s'" % strg)
if strg.find(".") == -1:
strg += ".000000"
if sys.version[0:3] >= "2.6":
return dt.datetime.strptime(strg, "%Y-%m-%dT%H:%M:%S.%f")
else:
dat, mis = strg.split(".")
dat = dt.datetime.strptime(dat, "%Y-%m-%dT%H:%M:%S")
mis = int(float("." + mis) * 1000000)
return dat.replace(microsecond=mis)
8 changes: 4 additions & 4 deletions 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 Expand Up @@ -128,11 +128,11 @@ def get(self, name=""):

def _check_age(self, pub, min_interval=zero_seconds):
"""Check the age of the receiver."""
now = dt.datetime.utcnow()
now = dt.datetime.now(dt.timezone.utc)
if (now - self._last_age_check) <= min_interval:
return

LOGGER.debug("%s - checking addresses", str(dt.datetime.utcnow()))
LOGGER.debug("%s - checking addresses", str(dt.datetime.now(dt.timezone.utc)))
self._last_age_check = now
to_del = []
with self._address_lock:
Expand Down Expand Up @@ -232,7 +232,7 @@ def set_up_address_receiver(self, port):
def _add(self, adr, metadata):
"""Add an address."""
with self._address_lock:
metadata["receive_time"] = dt.datetime.utcnow()
metadata["receive_time"] = dt.datetime.now(dt.timezone.utc)
self._addresses[adr] = metadata


Expand Down
8 changes: 3 additions & 5 deletions posttroll/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
except ImportError:
import simplejson as json

from posttroll import strp_isoformat

_MAGICK = "pytroll:/"
_VERSION = "v1.01"

Expand Down Expand Up @@ -132,7 +130,7 @@ def __init__(self, subject="", atype="", data="", binary=False, rawstr=None):
self.type = atype
self.type = atype
self.sender = _getsender()
self.time = dt.datetime.utcnow()
self.time = dt.datetime.now(dt.timezone.utc)
self.data = data
self.binary = binary
self.version = _VERSION
Expand Down Expand Up @@ -229,7 +227,7 @@ def datetime_decoder(dct):
for key, val in pairs:
if isinstance(val, str):
try:
val = strp_isoformat(val)
val = dt.datetime.fromisoformat(val)
except ValueError:
pass
elif isinstance(val, (dict, list)):
Expand All @@ -252,7 +250,7 @@ def _decode(rawstr):
msg = dict((("subject", raw[0].strip()),
("type", raw[1].strip()),
("sender", raw[2].strip()),
("time", strp_isoformat(raw[3].strip())),
("time", dt.datetime.fromisoformat(raw[3].strip())),
("version", version)))

# Data part
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
6 changes: 3 additions & 3 deletions posttroll/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ 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.utcnow() - self.lastbeat >=
(dt.datetime.now(dt.timezone.utc) - self.lastbeat >=
dt.timedelta(seconds=min_interval))):
self.lastbeat = dt.datetime.utcnow()
self.lastbeat = dt.datetime.now(dt.timezone.utc)
LOGGER.debug("Publish heartbeat (min_interval is %.1f sec)", min_interval)
self.publisher.send(Message(self.subject, "beat",
{"min_interval": min_interval}).encode())
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
1 change: 1 addition & 0 deletions posttroll/tests/data/message_metadata_aware.dumps
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"satellite": "metop2", "format": "hrpt", "timestamp": "2010-12-03T16:28:39+00:00", "afloat": 1.2344999999999999, "uri": "file://data/my/path/to/hrpt/files/myfile", "orbit": 1222}
File renamed without changes.
Loading

0 comments on commit 77f8410

Please sign in to comment.