summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2022-08-20 10:19:50 -0700
committerScott Gasch <[email protected]>2022-08-20 10:19:50 -0700
commite65c692279786d17bce47ebc2ce3c473f7d3bf27 (patch)
tree3eee4f8fcb9d117385b02ef31f6cb28c013e0fb2
parente855227db11fdd8a34e13ad3223aa9a5dd4b7978 (diff)
Fix bug in translate_timezone and improve test.
-rw-r--r--datetime_utils.py15
1 files 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: