将时区感知日期时间转换为Python中的本地时间
如何将时区感知的datetime对象转换为本地时区的等效非时区感知日期时间?
How do you convert a timezone-aware datetime object to the equivalent non-timezone-aware datetime for the local timezone?
我的特定应用程序使用Django(尽管如此,这实际上是一个通用的Python问题):
My particular application uses Django (although, this is in reality a generic Python question):
import iso8601
....
date_str="2010-10-30T17:21:12Z"
....
d = iso8601.parse_date(date_str)
foo = app.models.FooModel(the_date=d)
foo.save()
这将导致Django发生错误:
This causes Django to throw an error:
raise ValueError("MySQL backend does not support timezone-aware datetimes.")
我需要的是:
d = iso8601.parse_date(date_str)
local_d = SOME_FUNCTION(d)
foo = app.models.FooModel(the_date=local_d)
SOME_FUNCTION 是
一般来说,要将任意的时区感知日期时间转换为天真(本地)datetime,我会使用 pytz
模块和 astimezone
转换为本地时间,而替换
使datetime天真:
In general, to convert an arbitrary timezone-aware datetime to a naive (local) datetime, I'd use the pytz
module and astimezone
to convert to local time, and replace
to make the datetime naive:
In [76]: import pytz
In [77]: est=pytz.timezone('US/Eastern')
In [78]: d.astimezone(est)
Out[78]: datetime.datetime(2010, 10, 30, 13, 21, 12, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
In [79]: d.astimezone(est).replace(tzinfo=None)
Out[79]: datetime.datetime(2010, 10, 30, 13, 21, 12)
但是由于您的特定日期时间似乎在UTC时区,您可以这样做:
But since your particular datetime seems to be in the UTC timezone, you could do this instead:
In [65]: d
Out[65]: datetime.datetime(2010, 10, 30, 17, 21, 12, tzinfo=tzutc())
In [66]: import datetime
In [67]: import calendar
In [68]: datetime.datetime.fromtimestamp(calendar.timegm(d.timetuple()))
Out[68]: datetime.datetime(2010, 10, 30, 13, 21, 12)
顺便说一下,您可能会将数据时间保存为天真的UTC数据时间而不是天真的本地数据时间这样,您的数据与本地时间无关,您只能在必要时转换为本地时间或任何其他时区。类似于在unicode中工作的类型尽可能多,仅在需要时进行编码。
By the way, you might be better off storing the datetimes as naive UTC datetimes instead of naive local datetimes. That way, your data is local-time agnostic, and you only convert to local-time or any other timezone when necessary. Sort of analogous to working in unicode as much as possible, and encoding only when necessary.
所以如果你同意将数据时间存储在天真的UTC中是最好的方法,那么所有你需要做的是定义:
So if you agree that storing the datetimes in naive UTC is the best way, then all you'd need to do is define:
local_d = d.replace(tzinfo=None)