如何使python中不知道的datetime时区知道 [英] How to make an unaware datetime timezone aware in python

查看:422
本文介绍了如何使python中不知道的datetime时区知道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的

我有一个时区不知道的datetime对象,我需要添加一个时区以便与其他时区感知的日期时间对象进行比较。我不想将我的整个应用程序转换为这个传统案例的时区不知道。

I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezone-aware datetime objects. I do not want to convert my entire application to timezone unaware for this one legacy case.

我试过的

首先,为了演示问题:

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:

First, I tried 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
>>>

这不是惊人的失败,因为它实际上是在做一个转换。替换似乎是一个更好的选择(根据 Python:如何获取时区知道的时间datetime.today()值):

It's not terribly surprising this failed, since it's actually trying to do a conversion. Replace seemed like a better choice (as per Python: How to get a value of datetime.today() that is "timezone aware"?):

>>> 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
>>> 

但是您可以看到,替换似乎设置了tzinfo,但不能使对象感知到。我正在准备好回去修改输入字符串,以便在解析之前使用时区(我正在使用dateutil进行解析,如果重要),但这似乎令人难以置信的kludgy。

But as you can see, replace seems to set the tzinfo, but not make the object aware. I'm getting ready to fall back to doctoring the input string to have a timezone before parsing it (I'm using dateutil for parsing, if that matters), but that seems incredibly kludgy.

另外,我已经在python 2.6和python 2.7中尝试过,结果相同。

Also, I've tried this in both python 2.6 and python 2.7, with the same results.

上下文

我正在为一些数据文件编写一个解析器。有一种旧格式,我需要支持日期字符串没有时区指示符。我已经修复了数据源,但是我仍然需要支持旧的数据格式。遗留数据的一次性转换不是各种业务BS原因的选择。虽然一般来说,我不喜欢硬编码默认时区的想法,在这种情况下,它似乎是最好的选择。我以合理的信心知道所有遗留的数据都是UTC,所以我准备接受在这种情况下违约的风险。

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.

推荐答案

一般来说,要使一个天真的datetime时区感知,请使用本地化方法

In general, to make a naive datetime timezone-aware, use the localize method:

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时区,不需要使用 localize ,因为没有夏令时计算来处理:

For the UTC timezone, it is not really necessary to use localize since there is no daylight savings time calculation to handle:

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

的作品。 ( .replace 返回一个新的datetime;它不会修改 unaware 。)

works. (.replace returns a new datetime; it does not modify unaware.)

这篇关于如何使python中不知道的datetime时区知道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆