Django DateTimeField 存储日期时间而不考虑 tzinfo
Django DateTimeField Stores datetime regardless of the tzinfo
为什么 django DateTimeField
将 datetime
中的 tzinfo
恢复到 <utc>
?
下面是我的测试代码。
正常还是错误?
如果正常,请问是什么原因?
models.py
class Date(models.Model):
datetime = models.DateTimeField()
settings.py
TIME_ZONE = 'Asia/Seoul'
USE_TZ = True
test.py
from django.utils import timezone
datetime = timezone.localtime(timezone.localtimezone.now())
#now datetime is datetime.datetime(2015, 10, 22, 20, 31, 56, 248000, tzinfo=<DstTzInfo 'Asia/Seoul' KST+9:00:00 STD>)
models.Date(datetime=datetime).save()
#If I check datebase, It shows 2015-10-22 11:31:56.248000
model.Date.object.get(..)
#It returns datetime.datetime(2015, 10, 22, 11, 31, 56, 248000, tzinfo=<UTC>)
我希望 django 商店 2015-10-22 20:31:56.248000
和 return datetime.datetime(2015, 10, 22, 20, 31, 56, 248000, tzinfo=<DstTzInfo 'Asia/Seoul' KST+9:00:00 STD>)
----------------编辑----------------
我用了Sqlite
.
请务必阅读 Django's timezone documentation。该方法在第一句话中简要说明:
When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms.
所以,是的,您在 UTC 中看到数据库中的 return 值是正常的。
至于为什么,文档指出:
Even if your Web site is available in only one time zone, it’s still good practice to store data in UTC in your database. The main reason is Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions happen.
它还链接到 pytz
文档中的 a more detailed description。
为什么 django DateTimeField
将 datetime
中的 tzinfo
恢复到 <utc>
?
下面是我的测试代码。
正常还是错误?
如果正常,请问是什么原因?
models.py
class Date(models.Model):
datetime = models.DateTimeField()
settings.py
TIME_ZONE = 'Asia/Seoul'
USE_TZ = True
test.py
from django.utils import timezone
datetime = timezone.localtime(timezone.localtimezone.now())
#now datetime is datetime.datetime(2015, 10, 22, 20, 31, 56, 248000, tzinfo=<DstTzInfo 'Asia/Seoul' KST+9:00:00 STD>)
models.Date(datetime=datetime).save()
#If I check datebase, It shows 2015-10-22 11:31:56.248000
model.Date.object.get(..)
#It returns datetime.datetime(2015, 10, 22, 11, 31, 56, 248000, tzinfo=<UTC>)
我希望 django 商店 2015-10-22 20:31:56.248000
和 return datetime.datetime(2015, 10, 22, 20, 31, 56, 248000, tzinfo=<DstTzInfo 'Asia/Seoul' KST+9:00:00 STD>)
----------------编辑----------------
我用了Sqlite
.
请务必阅读 Django's timezone documentation。该方法在第一句话中简要说明:
When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms.
所以,是的,您在 UTC 中看到数据库中的 return 值是正常的。
至于为什么,文档指出:
Even if your Web site is available in only one time zone, it’s still good practice to store data in UTC in your database. The main reason is Daylight Saving Time (DST). Many countries have a system of DST, where clocks are moved forward in spring and backward in autumn. If you’re working in local time, you’re likely to encounter errors twice a year, when the transitions happen.
它还链接到 pytz
文档中的 a more detailed description。