2017-03-17 52 views
2

我得到以下輸出。這是Pytz的預期行爲嗎?順便說一下,我住在美國/東部時區。爲什麼EST將-04:56作爲時區偏移?pytz和datetime奇怪的行爲 - 可能的錯誤?

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年3月16日22:52:27.616000-04:56

PST - 版本1 2017年3月16日19:52:27.617000-07:00

PST - 版本2 2017年3月16日20:48:27.616000-07:00

+0

[在isoformat時區字段(的可能的複製http://stackoverflow.com/questions/26264897/time-區域中的字段) – DyZ

回答

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) 
+0

儘管此代碼可能會回答問題,但提供有關如何解決問題和/或爲何解決問題的其他上下文會提高答案的長期價值。 –