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

Make python timezone conversions handle more cases #5249

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
51 changes: 47 additions & 4 deletions py/server/deephaven/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,46 @@ def time_zone_alias_rm(alias: str) -> bool:

# region Conversions: Python To Java

def _tzinfo_to_j_time_zone(tzi: datetime.tzinfo, offset: datetime.timedelta) -> TimeZone:
"""
Converts a Python time zone to a Java TimeZone.

Args:
tzi: time zone info
offset: UTC offset

Returns:
Java TimeZone
"""
if not tzi:
return None

# Try to get the time zone from the zone name
try:
return _JDateTimeUtils.parseTimeZone(str(tzi))
Copy link
Contributor

Choose a reason for hiding this comment

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

datetime.tzinfo is an abstract class, so I'm not sure str(tzi) is guaranteed to be well-defined. It looks like tzinfo does not define a method for string conversion, so you hope that the subclass does: https://github.com/python/cpython/blob/a76288ad9ba715216a29c89e046846b3eaf0fab7/Lib/_pydatetime.py#L1231

except Exception:
pass

# Try to get the time zone from the UTC offset

if not offset:
raise ValueError("Unable to determine the time zone UTC offset")

if offset.microseconds != 0 or offset.seconds%60 != 0:
raise ValueError(f"Unsupported time zone offset contains fractions of a minute: {offset}")

ts = offset.total_seconds()

if ts >= 0:
sign = "+"
else:
sign = "-"
ts = -ts

hours = int(ts / 3600)
minutes = int((ts % 3600) / 60)
return _JDateTimeUtils.parseTimeZone(f"UTC{sign}{hours:02d}:{minutes:02d}")


def to_j_time_zone(tz: Union[None, TimeZone, str, datetime.tzinfo, datetime.datetime, pandas.Timestamp]) -> \
Optional[TimeZone]:
chipkent marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -192,12 +232,15 @@ def to_j_time_zone(tz: Union[None, TimeZone, str, datetime.tzinfo, datetime.date
elif isinstance(tz, str):
return _JDateTimeUtils.parseTimeZone(tz)
elif isinstance(tz, datetime.tzinfo):
return _JDateTimeUtils.parseTimeZone(str(tz))
return _tzinfo_to_j_time_zone(tz, tz.utcoffset(None) if tz else None)
elif isinstance(tz, datetime.datetime):
if not tz.tzname():
return _JDateTimeUtils.parseTimeZone(tz.astimezone().tzname())
tzi = tz.tzinfo
rst = _tzinfo_to_j_time_zone(tzi, tzi.utcoffset(tz) if tzi else None)

if not rst:
raise ValueError("datetime is not time zone aware")

return _JDateTimeUtils.parseTimeZone(tz.tzname())
return rst
else:
raise TypeError("Unsupported conversion: " + str(type(tz)) + " -> TimeZone")
except TypeError as e:
Expand Down
18 changes: 16 additions & 2 deletions py/server/tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def test_to_j_time_zone(self):
self.assertEqual(str(tz), "UTC")

pytz = datetime.datetime.now()
tz = to_j_time_zone(pytz)
self.assertEqual(str(tz), "UTC")
with self.assertRaises(DHError):
tz = to_j_time_zone(pytz)
self.fail("Expected DHError")

pytz = datetime.datetime.now().astimezone()
tz = to_j_time_zone(pytz)
Expand All @@ -93,6 +94,19 @@ def test_to_j_time_zone(self):
tz2 = to_j_time_zone(tz1)
self.assertEqual(tz1, tz2)

ts = pd.Timestamp("2022-07-07", tz="America/New_York")
self.assertEqual(to_j_time_zone(ts), to_j_time_zone("America/New_York"))

dttz = datetime.timezone(offset=datetime.timedelta(hours=5), name="XYZ")
chipkent marked this conversation as resolved.
Show resolved Hide resolved
dt = datetime.datetime(2022, 7, 7, 14, 21, 17, 123456, tzinfo=dttz)
self.assertEqual(to_j_time_zone(dttz), to_j_time_zone("UTC+5"))
self.assertEqual(to_j_time_zone(dt), to_j_time_zone("UTC+5"))

dttz = datetime.timezone(offset=-datetime.timedelta(hours=5), name="XYZ")
dt = datetime.datetime(2022, 7, 7, 14, 21, 17, 123456, tzinfo=dttz)
self.assertEqual(to_j_time_zone(dttz), to_j_time_zone("UTC-5"))
self.assertEqual(to_j_time_zone(dt), to_j_time_zone("UTC-5"))

with self.assertRaises(TypeError):
to_j_time_zone(False)
self.fail("Expected TypeError")
Expand Down
Loading