3

Using Python3, I do the following:

from datetime import datetime
from pytz import timezone
df = datetime(2018, 6, 1, hour=0, minute=0, tzinfo=timezone('US/Eastern'))
print(df)
2018-06-01 00:00:00-04:56

So, why does the date time get set to an offset of -4:56? Shouldn't it be -5:00? Why 4 minutes off?

Ira Klein
  • 425
  • 3
  • 6

1 Answers1

-1
eastern_tz = timezone('US/Eastern')
eastern_time = eastern_tz.localize(datetime(2018, 6, 1, 0, 0))

This gives you the correct result:

>>> print(eastern_time)
2018-06-01 00:00:00.000000-04:00

The reason is that python datetime module timezone objects are not directly compatible with pytz timezone objects. pytz documentation says:

if you want to create local wallclock times you need to use the localize() method documented in this document

nosklo
  • 217,122
  • 57
  • 293
  • 297