我有一个约会,我需要让时区知道.
local_tz = timezone('Asia/Tokyo')
start_date = '2012-09-27'
start_date = datetime.strptime(start_date, "%Y-%m-%d")
start_date = start_date.astimezone(local_tz)
now_utc = datetime.now(timezone('UTC'))
local_now = now_utc.astimezone(local_tz)
Run Code Online (Sandbox Code Playgroud)
我需要找到这是否属实:
print start_date>local_now
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个错误.
start_date = start_date.astimezone(local_tz)
ValueError: astimezone() cannot be applied to a naive datetime
Run Code Online (Sandbox Code Playgroud)
我将utc转换为tokyo没有任何问题.我需要在东京做好start_date时区感知广告.
谢谢
我试图理解为什么在将时区转换为UTC时我得到这些结果:
In [74]: d1 = datetime(2007, 12, 5, 6, 30,tzinfo=pytz.timezone('US/Pacific'))
In [75]: d1
Out[75]: datetime.datetime(2007, 12, 5, 6, 30, tzinfo=<DstTzInfo 'US/Pacific' LMT-1 day, **16:07:00 STD**>)
In [76]: d1.astimezone(pytz.utc)
Out[76]: datetime.datetime(2007, 12, 5, 14, 23, tzinfo=<UTC>)
Run Code Online (Sandbox Code Playgroud)
为什么早上6:30成为下午2:23?
另一方面,如果我使用以下方法,我得到预期的结果:
In [90]: d2 = datetime(2007, 12, 5, 6, 30)
In [91]: uspac = pytz.timezone('US/Pacific')
In [92]: d2_aware = uspac.localize(d2)
In [94]: d2_aware.astimezone(pytz.utc)
Out[94]: datetime.datetime(2007, 12, 5, 14, 30, tzinfo=<UTC>)
Run Code Online (Sandbox Code Playgroud)