diff --git a/betfairlightweight/resources/baseresource.py b/betfairlightweight/resources/baseresource.py index dfe96200..4ea958b6 100644 --- a/betfairlightweight/resources/baseresource.py +++ b/betfairlightweight/resources/baseresource.py @@ -3,6 +3,7 @@ from typing import Union, Optional from ..compat import basestring, integer_types, json, parse_datetime +from ..utils import utcfromtimestamp, utcnow class BaseResource: @@ -10,7 +11,7 @@ class BaseResource: def __init__(self, **kwargs): self.elapsed_time = kwargs.pop("elapsed_time", None) - now = datetime.datetime.utcnow() + now = utcnow() self._datetime_created = now self._datetime_updated = now self._data = kwargs @@ -31,7 +32,7 @@ def strip_datetime(value: Union[str, int]) -> Optional[datetime.datetime]: return elif isinstance(value, integer_types): try: - return datetime.datetime.utcfromtimestamp(value / 1e3) + return utcfromtimestamp(value / 1e3) except (ValueError, OverflowError, OSError): return diff --git a/betfairlightweight/streaming/betfairstream.py b/betfairlightweight/streaming/betfairstream.py index 2eae11e7..7a9c84e6 100644 --- a/betfairlightweight/streaming/betfairstream.py +++ b/betfairlightweight/streaming/betfairstream.py @@ -7,6 +7,7 @@ from ..exceptions import SocketError, ListenerError from ..compat import json +from ..utils import utcnow from .listener import BaseListener logger = logging.getLogger(__name__) @@ -225,7 +226,7 @@ def _read_loop(self) -> None: received_data_raw = self._receive_all() if self._running: self.receive_count += 1 - self.datetime_last_received = datetime.datetime.utcnow() + self.datetime_last_received = utcnow() received_data_split = received_data_raw.split(self.__CRLF) for received_data in received_data_split: if received_data: diff --git a/betfairlightweight/streaming/stream.py b/betfairlightweight/streaming/stream.py index fbba55ba..7460a7da 100644 --- a/betfairlightweight/streaming/stream.py +++ b/betfairlightweight/streaming/stream.py @@ -3,6 +3,7 @@ import time from typing import Optional +from ..utils import utcnow from .cache import CricketMatchCache, MarketBookCache, OrderBookCache, RaceCache logger = logging.getLogger(__name__) @@ -33,8 +34,8 @@ def __init__(self, listener: object, unique_id: int): self._updates_processed = 0 self._on_creation() - self.time_created = datetime.datetime.utcnow() - self.time_updated = datetime.datetime.utcnow() + self.time_created = utcnow() + self.time_updated = utcnow() def on_subscribe(self, data: dict) -> None: self._update_clk(data) @@ -147,7 +148,7 @@ def _update_clk(self, data: dict) -> None: self._initial_clk = initial_clk if clk: self._clk = clk - self.time_updated = datetime.datetime.utcnow() + self.time_updated = utcnow() @staticmethod def _calc_latency(publish_time: int) -> float: @@ -246,7 +247,6 @@ def _process(self, data: list, publish_time: int) -> bool: class RaceStream(BaseStream): - """ Cache contains latest update: marketId: RaceCache diff --git a/betfairlightweight/utils.py b/betfairlightweight/utils.py index cffb8eec..4e77d14f 100644 --- a/betfairlightweight/utils.py +++ b/betfairlightweight/utils.py @@ -88,3 +88,14 @@ def create_date_string(date: datetime.datetime) -> Optional[str]: """ if date: return date.strftime(BETFAIR_DATE_FORMAT) + + +def utcnow(): + """return timezone naive now""" + return datetime.datetime.now(tz=datetime.timezone.utc).replace(tzinfo=None) + + +def utcfromtimestamp(ts): + return datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc).replace( + tzinfo=None + )