0

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'?

1
  • 2
    From Python 3.9, use the built-in zoneinfo module instead of pytz. Commented Oct 26, 2024 at 21:24

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.