I ran into this problem a while back ago where parsing an ISO string with time zone data and parsing a timestamp (supposedly of the same time) will give different results. I have written a test to check this behavior and it seems pretty inconsistent:
from pytz import timezone as tz
from datetime import datetime
timezone = "Australia/Sydney"
start_time = "2021-05-04T08:12:00"
tz_object = tz(timezone)
naive_datetime = datetime.fromisoformat(start_time)
zoned_time = datetime(naive_datetime.year, naive_datetime.month, naive_datetime.day, naive_datetime.hour, naive_datetime.minute, naive_datetime.second, tzinfo=tz_object)
parsed_time = datetime.fromtimestamp(zoned_time.timestamp(), tz_object)
assert zoned_time.time() == naive_datetime.time()
assert zoned_time.time() == parsed_time.time()
This test produces the following output
File "test.py", line 13, in <module>
assert zoned_time.time() == parsed_time.time()
AssertionError
Unless I'm missing something I can only conclude that the time resulting from this line
parsed_time = datetime.fromtimestamp(zoned_time.timestamp(), tz_object)
Is producing a different time than parsing the actual ISO string. Typically I'd expect the timestamp from the parsed time return a timestamp referring to 8:12 am in the time zone it was assigned.
Is this behavior expected? Did I miss something?