0

I am trying to define a datetime object which represents January 1st, 2019 at 18:00 New York time.

I try:

import datetime
import pytz
ts = datetime.datetime(2019, 1, 1, 18, 0, 0, 0, tzinfo=pytz.timezone("US/Eastern"))
print(ts)

and I get:

2019-01-01 18:00:00-04:56

why -04:56?

I am using Python 3.7

M.E.
  • 4,955
  • 4
  • 49
  • 128
  • Does this answer your question? [Python pytz timezone function returns a timezone that is off by 9 minutes](https://stackoverflow.com/questions/35462876/python-pytz-timezone-function-returns-a-timezone-that-is-off-by-9-minutes) – jfaccioni Mar 03 '20 at 19:01
  • I do not think it does answer the question, but I will defintiively change the question title. I am totally confused, how do I generate a specific timestamp of a given timezone? i.e. what do I ahve to do in Python to create an object which represents the January 1st, 2019 at 18:00 New York time (Easter Time). – M.E. Mar 03 '20 at 19:09
  • This answers your question: https://stackoverflow.com/questions/26264897/time-zone-field-in-isoformat – Victor Mar 03 '20 at 19:10
  • Does this answer your question? [Time zone field in isoformat](https://stackoverflow.com/questions/26264897/time-zone-field-in-isoformat) – Victor Mar 03 '20 at 19:11

2 Answers2

1

This link (Time zone field in isoformat) provided by Victor in the comments above contains the answer.

I specifically include here the way to construct an object for a given timestamp.

eastern = pytz.timezone("US/Eastern")
print(eastern.localize(datetime.datetime(2019, 1, 1, 18)).isoformat())
M.E.
  • 4,955
  • 4
  • 49
  • 128
0

There tends to be problems using tzinfo with datetime constructors if the timezone has daylight savings transitions (see here). You could use the UTC timezone and convert to respective local times when displaying the time as a workaround.

Elliot Young
  • 291
  • 5
  • 5
  • My input data are timestamps from New York , if I understood you correctly you are saying ot store timestamps in UTC, what I want to store them with New York timestamps. – M.E. Mar 03 '20 at 19:17