4

I get the following output. Is this the intended behavior of pytz? I live in US/Eastern timezone, by the way. Why is EST giving -04:56 as the timezone offset?

import datetime
import pytz

a = datetime.datetime.now()
tz_est = pytz.timezone("US/Eastern")
a = a.replace(tzinfo=tz_est)
print("EST")
print(a)
print("\n")

b = datetime.datetime.now(pytz.timezone("US/Pacific"))
print("PST - version 1")
print(b)
print("\n")

tz_pst = pytz.timezone('US/Pacific')
c = tz_pst.normalize(a)
print("PST - version 2")
print(c)
print("\n")

EST 2017-03-16 22:52:27.616000-04:56

PST - version 1 2017-03-16 19:52:27.617000-07:00

PST - version 2 2017-03-16 20:48:27.616000-07:00

andyjslee
  • 589
  • 2
  • 5
  • 15
  • Possible duplicate of [Time zone field in isoformat](http://stackoverflow.com/questions/26264897/time-zone-field-in-isoformat) – DYZ Mar 17 '17 at 03:06

2 Answers2

0
import datetime
import pytz

a = datetime.datetime.now(pytz.timezone("US/Eastern"))
b = datetime.datetime.now()

pacific = pytz.timezone("US/Pacific")
c = pacific.localize(b)
d = pacific.normalize(a)

print(c)
print(d)
andyjslee
  • 589
  • 2
  • 5
  • 15
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Mar 19 '17 at 11:03
0

Use zoneinfo instead of pytz to get the expected behavior.

https://docs.python.org/3/library/zoneinfo.html

from zoneinfo import ZoneInfo

dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
Poulsbo
  • 648
  • 5
  • 18