[Fixed]-Why does python new york time zone display 4:56 instead 4:00?

28👍

You should instead do it like this:

ny_tz = timezone('America/New_York')
ny_time = ny_tz.localize(datetime(2014, 9, 4, 10, 30, 2, 294757))

This gives you the correct result:

>>> print ny_tz.localize(datetime(2014, 9, 4, 10, 30, 2, 294757))
2014-09-04 10:30:02.294757-04:00

Relevant pytz documentation section: http://pytz.sourceforge.net/#localized-times-and-date-arithmetic

What happens in your case is timezone being blindly attached to the datetime object, without knowing its year, month, etc. Because the date is not known, and it is impossible to determine what was the time legislation at the moment, should DST be in effect, etc., it is assumed that you just want the geographical time for New York, which you get.

The results may vary for different years. For example, daylight saving time was introduced in the US in 1918, so the results for the same date in 1917 and 1918 differ:

>>> print ny_tz.localize(datetime(1917, 9, 4, 10, 30, 2, 294757))
1917-09-04 10:30:02.294757-05:00
>>> print ny_tz.localize(datetime(1918, 9, 4, 10, 30, 2, 294757))
1918-09-04 10:30:02.294757-04:00

Leave a comment