Python 带有UTC偏移量的django DateTimeField?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14074696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:22:10  来源:igfitidea点击:

django DateTimeField with UTC offset?

pythondjangodjango-timezone

提问by frnhr

I have a model with a DateTimeField:

我有一个带有 DateTimeField 的模型:

deadline = models.DateTimeField(verbose_name="Valid unitl", null=True, blank=True)

Users should be allowed to input date, time and timezone info in the field. This is my desired format:

应允许用户在字段中输入日期、时间和时区信息。这是我想要的格式:

2012-12-31 23:30 +0430

I expect the time will get converted to UTC before storing to db. So I tried using a model form for that, but it throws Enter a valid date/time.validation error on that DateTimeField if I enter the value above.

我希望时间会在存储到 db 之前转换为 UTC。所以我尝试为此使用模型表单,但Enter a valid date/time.如果我输入上面的值,它会在该 DateTimeField 上引发验证错误。

This is in settings.py:

这是在settings.py中:

DATE_INPUT_FORMATS = ('%Y-%m-%d %H:%M %Z', )

What am I missing?

我错过了什么?

Edit:

编辑:

As per Видул Петров's suggestion, tried to use a form field:

根据 Видул Петров 的建议,尝试使用表单字段:

deadline2 = forms.DateTimeField(input_formats=['%Y-%m-%d %H:%M %Z',],

Got the same effect: Enter a valid date/time.

得到了同样的效果: Enter a valid date/time.

Edit 2

编辑 2

It appears that datetime can't handle the "%z" parameter. This throws a ValueError:

datetime 似乎无法处理“%z”参数。这会抛出一个 ValueError:

datetime.datetime.strptime(value, format)

So I tested it in console:

所以我在控制台中测试了它:

>>> import datetime
>>> datetime.datetime.strptime('2012-12-30 19:00 +0100', "%Y-%m-%d %H:%M %z")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M %z'

Also tried pytz:

也试过pytz:

>>> import pytz
>>> pytz.datetime.datetime.strptime('2012-12-30 19:00 +0100', "%Y-%m-%d %H:%M %z")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M %z'

I really feel this should work. Did I miss some part of the docs that says otherwise?

我真的觉得这应该有效。我是否错过了一些另有说明的文档?

回答by Chatri Sae-Tung

When you set USE_TZ = Truein your settings, Django stores date and time information in UTC in the database otherwise it will store naivedate time (date time without timezone).

当您USE_TZ = True在设置中进行设置时,Django 会在数据库中以 UTC 格式存储日期和时间信息,否则它将存储原始日期时间(不带时区的日期时间)。

In most cases using Django's time zones support is very convenient because input and output datetime will be automatically translate by Django.

在大多数情况下,使用 Django 的时区支持非常方便,因为输入和输出日期时间会由 Django 自动转换。

But if you really need timezone input from your user, you will need to set USE_TZ = Falsethen use DateTimeField which is naive datetime along with CharField to store timezone information in your models.py.

但是,如果您确实需要用户输入时区,则需要设置USE_TZ = False然后使用 DateTimeField 和 CharField 将时区信息存储在您的 models.py 中。

ref: https://docs.djangoproject.com/en/1.4/topics/i18n/timezones/

参考:https: //docs.djangoproject.com/en/1.4/topics/i18n/timezones/