繁体   English   中英

获取时区感知日期时间

[英]Get timezone aware datetime

我将如何执行以下操作以获取UTC日期时间对象,因此django不会抱怨/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2015-02-11 00:00:00) while time zone support is active.

>>> from django.utils import timezone
>>> timezone.date(2014,1,1)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'date'

以您的示例为例,您应该使用datetime并指定tzinfo参数:

timezone.datetime(2014, 1, 1, tzinfo=timezone.UTC())

您可能也对Django的make_aware函数感兴趣:

https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.timezone.make_aware

假设您输入的是没有时区信息的字符串,只需在字符串后附加文字"Z"即可。 这表示相对于UTC时间的零偏移。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM