From 3da3f0c2e2a467d3ae06a7988a930ddd6a960fd0 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Fri, 9 Jun 2023 13:45:39 +0100 Subject: [PATCH 1/4] Add datetime_to_publish_time function --- betfairutil/__init__.py | 14 +++++++++----- tests/test_non_prices.py | 9 +++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/betfairutil/__init__.py b/betfairutil/__init__.py index 311e82c..3e0d0e6 100644 --- a/betfairutil/__init__.py +++ b/betfairutil/__init__.py @@ -2748,11 +2748,15 @@ def g(): def publish_time_to_datetime( publish_time: Optional[int], ) -> Optional[datetime.datetime]: - if publish_time is None: - return - return datetime.datetime.utcfromtimestamp(publish_time / 1000).replace( - tzinfo=datetime.timezone.utc - ) + if publish_time is not None: + return datetime.datetime.utcfromtimestamp(publish_time / 1000).replace( + tzinfo=datetime.timezone.utc + ) + + +def datetime_to_publish_time(_datetime: Optional[datetime.datetime]) -> Optional[int]: + if _datetime is not None: + return int(_datetime.timestamp() * 1000) def create_market_book_generator_from_prices_file( diff --git a/tests/test_non_prices.py b/tests/test_non_prices.py index 76b17ff..52d6ab8 100644 --- a/tests/test_non_prices.py +++ b/tests/test_non_prices.py @@ -22,6 +22,7 @@ from betfairutil import convert_yards_to_metres from betfairutil import create_combined_market_book_and_race_change_generator from betfairutil import DataFrameFormatEnum +from betfairutil import datetime_to_publish_time from betfairutil import does_market_book_contain_runner_names from betfairutil import does_market_definition_contain_runner_names from betfairutil import EX_KEYS @@ -1305,3 +1306,11 @@ def test_get_total_volume_traded_from_prices_file( def test_publish_time_to_datetime(): assert publish_time_to_datetime(None) is None + + +def test_datetime_to_publish_time(market_book: dict[str, Any]): + assert datetime_to_publish_time(None) is None + assert ( + datetime_to_publish_time(publish_time_to_datetime(market_book["publishTime"])) + == market_book["publishTime"] + ) From 653731bb75c6063c0b495760f7fcf2d181601961 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Fri, 9 Jun 2023 13:46:45 +0100 Subject: [PATCH 2/4] Update HISTORY.md --- HISTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index c2bbe60..66f4a0c 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,11 @@ # Release History +# 0.6.2 - 2023-06-09 + +### Added + +* datetime_to_publish_time function + # 0.6.1 - 2023-06-06 ### Changed From d16150c964588059c14643f9b12120e1acf1009f Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Fri, 9 Jun 2023 13:47:00 +0100 Subject: [PATCH 3/4] Bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b1bfbf7..52837fa 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( name="betfairutil", - version="0.6.1", + version="0.6.2", description="Utility functions for working with Betfair data", long_description=long_description, long_description_content_type="text/markdown", From bda012aaff035415019e8d1f9994b390b0e4c082 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Fri, 9 Jun 2023 13:50:38 +0100 Subject: [PATCH 4/4] Fix type hint --- tests/test_non_prices.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_non_prices.py b/tests/test_non_prices.py index 52d6ab8..e69f49a 100644 --- a/tests/test_non_prices.py +++ b/tests/test_non_prices.py @@ -1308,7 +1308,7 @@ def test_publish_time_to_datetime(): assert publish_time_to_datetime(None) is None -def test_datetime_to_publish_time(market_book: dict[str, Any]): +def test_datetime_to_publish_time(market_book: Dict[str, Any]): assert datetime_to_publish_time(None) is None assert ( datetime_to_publish_time(publish_time_to_datetime(market_book["publishTime"]))