From e65c692279786d17bce47ebc2ce3c473f7d3bf27 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Sat, 20 Aug 2022 10:19:50 -0700 Subject: [PATCH] Fix bug in translate_timezone and improve test. --- datetime_utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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: -- 2.47.1