Input:
import pytz
from datetime import datetime as dt
targettz = pytz.timezone('America/New_York')
d1 = dt(2024, 1, 1, 7, 40, 0, tzinfo=targettz)
d1.isoformat()
Output:
'2024-01-01T07:40:00-04:56'
Why '-04:56' but not '-04:00'?
As mentioned by @MT0 using zoneinfo works correctly:
>>> dt(2024, 1, 1, 7, 40, 0, tzinfo=pytz.timezone('America/New_York')).isoformat()
'2024-01-01T07:40:00-04:56'
>>> dt(2024, 1, 1, 7, 40, 0, tzinfo=zoneinfo.ZoneInfo('America/New_York')).isoformat()
'2024-01-01T07:40:00-05:00'
>>> dt(2024, 10, 10, 7, 40, 0, tzinfo=zoneinfo.ZoneInfo('America/New_York')).isoformat()
'2024-10-10T07:40:00-04:00'
In January, NY observes winter time, which is UTC-5, and therefore the offset is correct. If you want to see the -04:00 offset, change date to one where DST is observed, like Oct 10.
zoneinfomodule instead ofpytz.