如何在python中使不知情的日期时区知道
我需要做的是
我试过的
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import datetime>>> import pytz>>> unaware = datetime.datetime(2011,8,15,8,15,12,0)>>> unaware datetime.datetime(2011, 8, 15, 8, 15, 12)>>> aware = datetime.datetime(2011,8,15,8,15,12,0,pytz.UTC)>>> aware datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=<UTC>)>>> aware == unawareTraceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: can't compare offset-naive and offset-aware datetimes
>>> unaware.astimezone(pytz.UTC)Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: astimezone() cannot be applied to a naive datetime>>>
>>> unaware.replace(tzinfo=pytz.UTC)datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=<UTC>) >>> unaware == awareTraceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: can't compare offset-naive and offset-aware datetimes >>>
语境
呼如林
守着星空守着你
jeck猫
相关分类