小编典典

在Python中将时区感知日期时间转换为本地时间

django

如何将时区感知日期时间对象转换为本地时区的等效非时区感知日期时间?

我的特定应用程序使用Django(尽管这实际上是一个通用的Python问题):

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抛出错误:

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是什么?


阅读 487

收藏
2020-04-02

共1个答案

小编典典

通常,要将任意时区感知日期时间转换为原始(本地)日期时间,我将使用pytz模块并将astimezone其转换为本地时间,replace并使日期时间变为原始状态:

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时区,因此你可以这样做:

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,并且仅在必要时进行编码。

因此,如果你同意将日期时间存储在幼稚的UTC中是最好的方法,那么你只需定义:

local_d = d.replace(tzinfo=None)
2020-04-02