我如何执行以下操作来获取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'
答案 0 :(得分:1)
举个例子,你应该使用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
答案 1 :(得分:0)
假设您的输入是一个没有时区信息的字符串,只需在字符串中添加文字"Z"
即可。这表示与UTC时间的零偏移。