我需要做什么

我有一个不了解时区的datetime对象,我需要向它添加一个时区,以便能够将它与其他了解时区的datetime对象进行比较。我不想将我的整个应用程序转换为不知道这个遗留情况的时区。

我的努力

首先,演示问题:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "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 == unaware
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware datetimes

首先,我尝试了astimezone:

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: astimezone() cannot be applied to a naive datetime
>>>

这个失败并不奇怪,因为它实际上是在尝试进行转换。Replace似乎是一个更好的选择(如如何在Python中获得“时区感知”的datetime.today()值?):

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=<UTC>)
>>> unaware == aware
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 

但正如您所看到的,replace似乎设置了tzinfo,但并没有使对象感知。我准备在解析输入字符串之前修改输入字符串以获得时区(如果有问题的话,我将使用dateutil进行解析),但这似乎非常笨拙。

此外,我在Python 2.6和Python 2.7中都尝试了这一点,得到了相同的结果。

上下文

I am writing a parser for some data files. There is an old format I need to support where the date string does not have a timezone indicator. I've already fixed the data source, but I still need to support the legacy data format. A one time conversion of the legacy data is not an option for various business BS reasons. While in general, I do not like the idea of hard-coding a default timezone, in this case it seems like the best option. I know with reasonable confidence that all the legacy data in question is in UTC, so I'm prepared to accept the risk of defaulting to that in this case.


当前回答

我刚接触Python,遇到了同样的问题。我发现这个解决方案非常简单,对我来说它工作得很好(Python 3.6):

unaware=parser.parse("2020-05-01 0:00:00")
aware=unaware.replace(tzinfo=tz.tzlocal()).astimezone(tz.tzlocal())

其他回答

所有这些例子都使用了一个外部模块,但你可以只使用datetime模块来实现相同的结果,正如下面的SO答案所示:

from datetime import datetime, timezone

dt = datetime.now()
dt = dt.replace(tzinfo=timezone.utc)

print(dt.isoformat())
# '2017-01-12T22:11:31+00:00'

更少的依赖,没有pytz问题。

注意:如果你想在python3和python2中使用它,你也可以在时区导入中使用它(为UTC硬编码):

try:
    from datetime import timezone
    utc = timezone.utc
except ImportError:
    #Hi there python2 user
    class UTC(tzinfo):
        def utcoffset(self, dt):
            return timedelta(0)
        def tzname(self, dt):
            return "UTC"
        def dst(self, dt):
            return timedelta(0)
    utc = UTC()

对于那些只是想使一个时区感知datetime

import datetime

datetime.datetime(2019, 12, 7, tzinfo=datetime.timezone.utc)

对于那些在python 3.9 stdlib中希望使用非utc时区的datetime的人

import datetime
from zoneinfo import ZoneInfo

datetime.datetime(2019, 12, 7, tzinfo=ZoneInfo("America/Los_Angeles")) 

我在2011年写了这个Python 2脚本,但从未检查过它是否能在Python 3上运行。

我已经从dt_aware移动到dt_unconscious:

dt_unaware = dt_aware.replace(tzinfo=None)

dt_unware到dt_aware:

from pytz import timezone
localtz = timezone('Europe/Lisbon')
dt_aware = localtz.localize(dt_unware)

一般来说,要使naive datetime具有时区感知,请使用localalize方法:

import datetime
import pytz

unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0)
aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC)

now_aware = pytz.utc.localize(unaware)
assert aware == now_aware

对于UTC时区,实际上没有必要使用本地化,因为没有日光节约时间计算要处理:

now_aware = unaware.replace(tzinfo=pytz.UTC)

的工作原理。(.Replace返回一个新的日期时间;它不会无意识地修改。)

以上所有提到的方法,当它是Unix时间戳时,有一个非常简单的解决方案使用pandas。

import pandas as pd

unix_timestamp = 1513393355
pst_tz = pd.Timestamp(unix_timestamp, unit='s', tz='US/Pacific')
utc_tz = pd.Timestamp(unix_timestamp, unit='s', tz='UTC')