Use of standard vs. timezone-aware date
types
#1306
Replies: 2 comments 2 replies
-
That is a valid approach that not only keeps performance, but also unifies the usage of the library across projects: If we only allow UTC-times, there are no more questions and possible bugs because of different time zones. @DaveSkender I also suggest to add a UTC timezone check to |
Beta Was this translation helpful? Give feedback.
-
Hi @DaveSkender, impressive analysis! I couldn't see any updates in other related discussions as well. This one still impacts my workflow I really would appreciate giving another look for this one. I don't have enough knowledge on the .NET implementation. Thus, I didn't look in the original implementation. Just as a user's perspective, it feels it should be handled in the .NET implementation. As I understand in the draft MR, retraining timezone awareness in Python "bridge" might add some overload as you have already mentioned. According to my observation, .NET-side converts the time by the host's timezome to host's timezone but also is aware of the timezone. To work with the library I had to apply my utility function to return values of the indicators. As you can imagine it creates difficulties while working with the library. from datetime import datetime as dt, timezone as tz
from stock_indicators._cstypes import DateTime as CsDateTime
from stock_indicators._cslib import CsCultureInfo
local_tz = dt.now(tz.utc).astimezone().tzinfo
def make_tz_aware(date: dt):
return date.replace(tzinfo=local_tz).astimezone(tz.utc)
py_now = dt.now(tz=tz.utc)
print('py_now', py_now)
cs_now = CsDateTime(py_now)
print('cs_now', cs_now)
cs_now_tz_naive = cs_now.ToString("s", CsCultureInfo.InvariantCulture)
print('cs_now_tz_naive', cs_now_tz_naive)
print('cs_now_tz_naive_processed', make_tz_aware(dt.fromisoformat(cs_now_tz_naive)))
cs_now_tz_aware = cs_now.ToString("o", CsCultureInfo.InvariantCulture)
print('cs_now_tz_aware', cs_now_tz_aware)
print('cs_now_tz_aware_processed', make_tz_aware(dt.fromisoformat(cs_now_tz_aware))) Logs
I believe expected behaviour should be the exactly what's given as input. Looking forward to any improvements on this issue. |
Beta Was this translation helpful? Give feedback.
-
A user raised a valid point about needing timezone awareness or offsets. I appreciate the opportunity to discuss further and to get community feedback. Let me explain the current approach of using the simpler form of
DateTime
and the rationale behind it.Why we use plain
datetime
data typesIndustry norms: Financial market data is typically timestamped relative to the exchange's local timezone and rarely includes timezone offsets. For example:
Given this, plain
datetime
objects align with the majority of use cases and data sources.Performance and simplicity: timezone-aware types (e.g.,
DateTimeOffset
in .NET ordatetime
withtzinfo
in Python) add overhead in terms of size, computation, and complexity. For a performance-focused library, this trade-off is significant, especially when handling large datasets.User-controlled normalization: The library assumes that users normalize data (e.g., to UTC) in their pre-processing pipelines. This keeps the library lightweight and focused on its core purpose—calculations and indicators.
Addressing concerns
We do recognize that timezone-aware dates could benefit use cases involving multiple exchanges or explicit timezone handling. However, introducing timezone support across the library would require careful consideration to avoid unintended side effects, such as increased memory usage, reduced performance, or breaking existing user workflows.
Possible Next Steps
Originally suggested by @goulashsoup in a bug report for our Python library
Beta Was this translation helpful? Give feedback.
All reactions