From: Scott Gasch Date: Sat, 20 Aug 2022 17:19:50 +0000 (-0700) Subject: Fix bug in translate_timezone and improve test. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=e65c692279786d17bce47ebc2ce3c473f7d3bf27;p=python_utils.git Fix bug in translate_timezone and improve test. --- diff --git a/datetime_utils.py b/datetime_utils.py index 55e0ffa..10b1666 100644 --- a/datetime_utils.py +++ b/datetime_utils.py @@ -200,19 +200,22 @@ def translate_timezone(dt: datetime.datetime, tz: datetime.tzinfo) -> datetime.d day, hour, minute, second, micro, etc... appropriately. The returned dt is the same instant in another timezone. - >>> from pytz import UTC + >>> import pytz >>> d = now_pacific() >>> d.tzinfo.tzname(d)[0] # Note: could be PST or PDT 'P' >>> h = d.hour - >>> o = translate_timezone(d, UTC) - >>> o.tzinfo.tzname(o) - 'UTC' + >>> o = translate_timezone(d, pytz.timezone('US/Eastern')) + >>> o.tzinfo.tzname(o)[0] # Again, could be EST or EDT + 'E' >>> o.hour == h False - + >>> expected = h + 3 # Three hours later in E?T than P?T + >>> expected = expected % 24 # Handle edge case + >>> expected == o.hour + True """ - return dt.replace(tzinfo=None).astimezone(tz=tz) + return dt.replace().astimezone(tz=tz) def now() -> datetime.datetime: