The total second difference between 2 datetime objects set in different time zones but on the same date does not yield 0.0 in some circumstances.
import datetime
from pytz import timezone
from dateutil import parser
print(datetime.datetime.now(timezone('US/Eastern')))
print(datetime.datetime.now(timezone('America/Vancouver')))
print((datetime.datetime.now(timezone('US/Eastern')) -datetime.datetime.now(timezone('America/Vancouver'))).total_seconds() )
print((datetime.datetime(2011, 8, 15, 8, 0, 0, 0, timezone('America/Vancouver')) - datetime.datetime(2011, 8, 15, 11, 0, 0, 0, timezone('US/Eastern'))).total_seconds() )
print((datetime.datetime(2011, 8, 15, 8, 0, 0, 0, timezone('America/Vancouver')) - datetime.datetime(2011, 8, 15, 11, 16, 0, 0, timezone('US/Eastern'))).total_seconds())
print((parser.parse("2011-08-15 08:00:00 UTC-4") - parser.parse("2011-08-15 11:00:00 UTC-7")).total_seconds())
For the third, fourth, and last output of the print statements I would expect them to be all 0.0. I would expect the fifth to be 960.0. However, it seems that when finding the difference of the same date in different time zones using a specific datetime.datetime constructor, it leads to a timezone difference of 3h and 16 minutes between US/Eastern and America/Vancouver.
My output is below:
2024-07-22 12:02:44.950826-04:00
2024-07-22 09:02:44.951827-07:00
0.0
960.0
0.0
0.0
Am I doing something wrong? I’m sure there is a proper explanation. Thank you for your help!
root-of-a-tree is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.