姬長信(Redy)

python – 什么时候`datetime.now(pytz_timezone)`失…


delorean docs显示这种方式获取给定时区using datetime的当前时间:
from datetime import datetime
from pytz import timezone

EST = "US/Eastern"
UTC = "UTC"

d = datetime.utcnow()
utc = timezone(UTC)
est = timezone(EST)
d = utc.localize(d)
d = est.normalize(EST)

并将其与基于delorian的代码进行比较:

from delorean import Delorean

EST = "US/Eastern"

d = Delorean(timezone=EST)

I believe datetime示例应写为:

from datetime import datetime
import pytz

eastern_timezone = pytz.timezone("US/Eastern")
d = datetime.now(eastern_timezone)

这更简洁.

当最后一个代码示例失败而第一个代码示例继续工作时,是否有任何情况?

更新:the current example:

from datetime import datetime
import pytz

d = datetime.utcnow()
d = pytz.utc.localize(d)

est = pytz.timezone('US/Eastern')
d = est.normalize(d)
return d

这仍然太冗长了.

问题仍然存在:do you need the explicit round-trip via utc and tz.normalize() or can you use datetime.now(tz) instead?