From 305b8e37885e4b9abb0a0ac920a160a0cae56583 Mon Sep 17 00:00:00 2001 From: Dave Skender <8432125+DaveSkender@users.noreply.github.com> Date: Tue, 31 Dec 2024 23:18:05 -0500 Subject: [PATCH] Update `DateTime` class and `test_quote_constructor_retains_timezone` method to handle timezone-aware datetime objects * **`stock_indicators/_cstypes/datetime.py`** - Update `DateTime` class to convert timezone-aware datetime objects to UTC - Update `to_pydatetime` function to retain timezone information when converting C# `System.DateTime` objects back to Python `datetime` objects * **`tests/common/test_cstype_conversion.py`** - Add `test_quote_constructor_retains_timezone` method to verify the retention of timezone information in the `Quote` constructor --- stock_indicators/_cstypes/datetime.py | 2 ++ tests/common/test_cstype_conversion.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/stock_indicators/_cstypes/datetime.py b/stock_indicators/_cstypes/datetime.py index 1a2ee63a..ac9a6715 100644 --- a/stock_indicators/_cstypes/datetime.py +++ b/stock_indicators/_cstypes/datetime.py @@ -20,6 +20,8 @@ class DateTime: 3/26/2021 10:02:22 PM """ def __new__(cls, datetime: PyDateTime) -> CsDateTime: + if datetime.tzinfo is not None: + datetime = datetime.astimezone(PyDateTime.timezone.utc) return CsDateTime.Parse(datetime.isoformat(timespec='seconds')) diff --git a/tests/common/test_cstype_conversion.py b/tests/common/test_cstype_conversion.py index 63cf6e4d..6b158c61 100644 --- a/tests/common/test_cstype_conversion.py +++ b/tests/common/test_cstype_conversion.py @@ -46,7 +46,7 @@ def test_auto_conversion_from_double_to_float(self): def test_quote_constructor_retains_timezone(self): dt = datetime.fromisoformat('2000-03-26 23:00+0000') q = Quote( - date=dt, + date=dt.astimezone(datetime.timezone.utc), open=Decimal('23'), high=Decimal('26'), low=Decimal('20'),