I have sensor data with a timestamp known to be in PST (Pacific Standard Time). Let's say that the timestamp is: 2020-10-21 10:00:00. I want to convert that timestamp to UTC. UTC-to-PST conversion is UTC-8. So the expected timestamp in UTC is 8 hours forward: 2020-10-21 18:00:00.
I tried to do the conversion:
>>> import pytz
>>> from datetime import datetime
>>> ts = datetime(2020,10,21,10,0,0)
>>> ts_local = pytz.timezone('America/Los_Angeles').localize(ts, is_dst=False)
>>> ts_local.strftime('%Y-%m-%d %H:%M:%S %Z%z')
'2020-10-21 10:00:00 PDT-0700'
>>> ts_local.astimezone(pytz.utc).strftime('%Y-%m-%d %H:%M:%S %Z%z')
'2020-10-21 17:00:00 UTC+0000'
But you can see that the conversion is 1 hour behind what is expected. This seems to make sense since ts_local appears to be in PDT. However, because I'd set is_dst=False, I expected ts_local to be in PST.
- I've tried
is_dst=Trueandis_dst=None(even though those flag values don't make sense since the timestamp is in Standard Time) but the resultingts_localremains inPDT. - I've tried this conversion in Python 3.7.4 and Python 2.7.17 and gotten the same result.
- I've also tried this conversion using the
'US/Pacific'pytz timezone but same issue.
I've read some other SO answers for how to do the conversion and what I've done is what has been suggested.