Skip to content

Commit

Permalink
Update DateTime class and test_quote_constructor_retains_timezone
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
DaveSkender committed Jan 1, 2025
1 parent dfbad48 commit 305b8e3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions stock_indicators/_cstypes/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))


Expand Down
2 changes: 1 addition & 1 deletion tests/common/test_cstype_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 305b8e3

Please sign in to comment.