projects
/
python_utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
e855227
)
Fix bug in translate_timezone and improve test.
author
Scott Gasch
<
[email protected]
>
Sat, 20 Aug 2022 17:19:50 +0000
(10:19 -0700)
committer
Scott Gasch
<
[email protected]
>
Sat, 20 Aug 2022 17:19:50 +0000
(10:19 -0700)
datetime_utils.py
patch
|
blob
|
history
diff --git
a/datetime_utils.py
b/datetime_utils.py
index 55e0ffab3b0dcedf1e6830aef38cd387d736fde0..10b166605570b14332fe7c7f5ebf74b645a05960 100644
(file)
--- 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.
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
>>> 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
>>> 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:
def now() -> datetime.datetime: