Skip to content

Commit

Permalink
fix(python): don't use zoneinfo globally (#5246)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 18, 2022
1 parent 56349fb commit 48376b5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions py-polars/polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ def _prepare_row_count_args(


EPOCH = datetime(1970, 1, 1).replace(tzinfo=None)
UTC = zoneinfo.ZoneInfo("UTC")


def _to_python_datetime(
Expand Down Expand Up @@ -226,16 +225,22 @@ def _to_python_datetime(
else:
raise ValueError(f"tu must be one of {{'ns', 'us', 'ms'}}, got {tu}")
else:
if not _ZONEINFO_AVAILABLE:
raise ImportError(
"Install polars[timezone] to handle datetimes with timezones."
)

utc = zoneinfo.ZoneInfo("UTC")
if tu == "ns":
# nanoseconds to seconds
dt = datetime.fromtimestamp(0, tz=UTC) + timedelta(
dt = datetime.fromtimestamp(0, tz=utc) + timedelta(
microseconds=value / 1000
)
elif tu == "us":
dt = datetime.fromtimestamp(0, tz=UTC) + timedelta(microseconds=value)
dt = datetime.fromtimestamp(0, tz=utc) + timedelta(microseconds=value)
elif tu == "ms":
# milliseconds to seconds
dt = datetime.fromtimestamp(value / 1000, tz=UTC)
dt = datetime.fromtimestamp(value / 1000, tz=utc)
else:
raise ValueError(f"tu must be one of {{'ns', 'us', 'ms'}}, got {tu}")
return _localize(dt, tz)
Expand Down

0 comments on commit 48376b5

Please sign in to comment.