Skip to content

Commit c0f3806

Browse files
committed
Refactor timestamp conversion to use timezone-aware datetime
1 parent f978c64 commit c0f3806

File tree

2 files changed

+2
-6
lines changed

2 files changed

+2
-6
lines changed

src/forest/poplar/functions/time.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ def to_readable(timestamp: int, to_format: str,
124124
try:
125125
if isinstance(to_tz, str):
126126
to_tz = pytz.timezone(to_tz)
127-
datetime_date = datetime.datetime.utcfromtimestamp(timestamp / 1000)
128-
utc_dt = pytz.utc.localize(datetime_date)
127+
utc_dt = datetime.datetime.fromtimestamp(timestamp / 1000, tz=pytz.UTC)
129128
local_dt = utc_dt.astimezone(to_tz)
130129
readable = local_dt.strftime(to_format)
131130
return readable

src/forest/poplar/legacy/common_funcs.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ def stamp2datetime(stamp: Union[float, int], tz_str: str) -> list:
5656
specified tz
5757
"""
5858
loc_tz = timezone(tz_str)
59-
60-
utc = timezone("UTC")
61-
utc_dt = utc.localize(datetime.utcfromtimestamp(stamp))
62-
59+
utc_dt = datetime.fromtimestamp(stamp, timezone("UTC"))
6360
loc_dt = utc_dt.astimezone(loc_tz)
6461
return [
6562
loc_dt.year, loc_dt.month, loc_dt.day,

0 commit comments

Comments
 (0)