Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Retain timezone awareness #410

Draft
wants to merge 26 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bb1df51
fix: Retain timezone awareness
DaveSkender Jan 1, 2025
8558dfb
Update `_get_date` and `_set_date` methods to handle timezone-aware d…
DaveSkender Jan 1, 2025
dfbad48
Update `_set_date` function to handle timezone information in `quote.py`
DaveSkender Jan 1, 2025
305b8e3
Update `DateTime` class and `test_quote_constructor_retains_timezone`…
DaveSkender Jan 1, 2025
d6e1bbe
DaveSkender Jan 1, 2025
e15697c
Update `_get_date` and `_set_date` functions to handle timezone-aware…
DaveSkender Jan 1, 2025
963abe4
isolate location of conversion
DaveSkender Jan 1, 2025
355241a
Update `DateTime` class and `to_pydatetime` function to handle timezo…
DaveSkender Jan 1, 2025
c4b5d70
Update `DateTime` class and `to_pydatetime` function to handle timezo…
DaveSkender Jan 1, 2025
be18867
Update `DateTime` class and `to_pydatetime` function to preserve time…
DaveSkender Jan 1, 2025
d08c3e5
Update `DateTime` class and `to_pydatetime` function to handle timezo…
DaveSkender Jan 1, 2025
44162a3
Update `DateTime` class and `Quote` class to handle timezone-aware da…
DaveSkender Jan 1, 2025
06305e4
Update `DateTime` class and `to_pydatetime` function to handle timezo…
DaveSkender Jan 1, 2025
bc8c0b5
Update `_get_date` and `_set_date` functions in `quote.py` to handle …
DaveSkender Jan 1, 2025
572cab0
add devcontainer
DaveSkender Jan 1, 2025
9f81b5d
update dev container
DaveSkender Jan 1, 2025
3bdb39b
Update `_set_date` function to handle timezone-aware datetime objects…
DaveSkender Jan 1, 2025
c07b38e
Update `DateTime` class to retain timezone information
DaveSkender Jan 1, 2025
fd70f30
Update `devcontainer.json` to include GitHub Copilot extension
DaveSkender Jan 1, 2025
8a6cb26
Update test cases to handle timezone-aware datetime objects
DaveSkender Jan 1, 2025
31d214b
Merge branch 'main' into DaveSkender/retain-timezone
DaveSkender Jan 1, 2025
8794fa8
Merge branch 'main' into DaveSkender/retain-timezone
DaveSkender Jan 22, 2025
ae22ee7
Update tests/common/test_cstype_conversion.py
DaveSkender Jan 25, 2025
73b3aeb
Related to #409
DaveSkender Jan 25, 2025
309202d
Merge branch 'main' into DaveSkender/retain-timezone
LeeDongGeon1996 Feb 12, 2025
dbca91d
Merge branch 'main' into DaveSkender/retain-timezone
DaveSkender Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stock_indicators/_cstypes/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DateTime:
3/26/2021 10:02:22 PM
"""
def __new__(cls, datetime: PyDateTime) -> CsDateTime:
return CsDateTime.Parse(datetime.isoformat())
return CsDateTime.Parse(datetime.isoformat(timespec='seconds'))


def to_pydatetime(cs_datetime: CsDateTime) -> PyDateTime:
Expand All @@ -30,4 +30,4 @@ def to_pydatetime(cs_datetime: CsDateTime) -> PyDateTime:
Parameter:
cs_datetime : `System.DateTime` of C#.
"""
return PyDateTime.fromisoformat(cs_datetime.ToString("s", CsCultureInfo.InvariantCulture))
return PyDateTime.fromisoformat(cs_datetime.ToString("o", CsCultureInfo.InvariantCulture))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o format won't work according to #310

16 changes: 16 additions & 0 deletions tests/common/test_cstype_conversion.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import datetime
from numbers import Number
from decimal import Decimal

from stock_indicators._cslib import CsCultureInfo
from stock_indicators._cstypes import DateTime as CsDateTime
from stock_indicators._cstypes import to_pydatetime
from stock_indicators.indicators.common.quote import Quote

class TestCsTypeConversion:
def test_datetime_conversion(self):
Expand Down Expand Up @@ -40,3 +42,17 @@ def test_auto_conversion_from_double_to_float(self):
assert isinstance(cs_double, Number)
assert isinstance(cs_double, float)
assert 1996.1012 == cs_double

def test_quote_constructor_retains_timezone(self):
dt = datetime.fromisoformat('2000-03-26 23:00+0000')
q = Quote(
date=dt,
open=Decimal('23'),
high=Decimal('26'),
low=Decimal('20'),
close=Decimal('25'),
volume=Decimal('323')
)

assert str(q.date.tzinfo) == 'UTC'
assert str(q.date.time()) == '23:00:00'
Loading