简体   繁体   English

Python / Django MySQL日期时间处理和时区

[英]Python/Django MySQL Datetime handling and timezone

I've got a model called Vote with a field date : 我有一个名为Vote的模型,带有一个字段date

date = models.DateTimeField(auto_now_add=True)

When I add an element, the date in MySQL is an UTC date but I live in UTC+2 timezone 当我添加元素时,MySQL中的日期是UTC日期,但我居住在UTC + 2时区

I think I correctly set the timezone in settings.py : 我想我在settings.py正确设置了时区:

TIME_ZONE = 'Europe/Paris'

Python use the right timezone : Python使用正确的时区:

>>> print datetime.datetime.now()
2013-07-03 09:05:04.474000

MySQL too : MySQL也是:

> SELECT NOW( )
2013-07-03 09:00:48

I could set the date attribute manualy, it works but I would like to know why auto_now_add return a wrong date although python and mysql use the right timezone 我可以手动设置日期属性,它可以工作,但我想知道为什么auto_now_add返回错误的日期虽然python和mysql使用正确的时区

Thank you 谢谢

It's a complex binding to explain. 这是一个复杂的解释。 From Django 1.4 , 来自Django 1.4

When USE_TZ is False, this is the time zone in which Django will store all datetimes. 当USE_TZ为False时, 是Django存储所有日期时间的时区。 When USE_TZ is True, this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms. 当USE_TZ为True时, 是Django用于在模板中显示日期时间和解释在表单中输入的日期时间的默认时区。

this refers to TIME_ZONE . 指的是TIME_ZONE So what is your USE_TZ ? 那你的USE_TZ什么? If your USE_TZ is True then Django will store datetime in UTC and use TIME_ZONE to display in templates and interpret forms. 如果您的USE_TZ为True,则Django将以UTC格式存储日期时间,并使用TIME_ZONE显示模板和解释表单。

This is because, if you change your TIME_ZONE later when hosting your site in another territory, it's easy to convert any datetimes from UTC to any timezones given. 这是因为,如果您稍后在另一个地区托管您的网站时更改了TIME_ZONE ,则很容易将任何日期时间从UTC转换为给定的任何时区。

In Django 1.3 and earlier, Django 1.3及更早版本中,

Note that this is the time zone to which Django will convert all dates/times – not necessarily the timezone of the server. 请注意, 是Django将转换所有日期/时间的时区 - 不一定是服务器的时区。 For example, one server may serve multiple Django-powered sites, each with a separate time-zone setting. 例如,一台服务器可以为多个Django供电的站点提供服务,每个站点都有一个单独的时区设置。

Normally, Django sets the os.environ['TZ'] variable to the time zone you specify in the TIME_ZONE setting. 通常,Django将os.environ ['TZ']变量设置为您在TIME_ZONE设置中指定的时区。 Thus, all your views and models will automatically operate in the correct time zone. 因此,您的所有视图和模型将自动在正确的时区内运行。

But doesn't tell you in what timezone the datetime will be stored in database. 但是没有告诉你datetime将在什么时区存储在数据库中。 Need to experiment anyway(my guess is UTC). 无论如何都需要进行实验(我的猜测是UTC)。

print datetime.datetime.now() prints the datatime according to the timezone setup of your server machine unless you have opened the python console via manage.py shell . print datetime.datetime.now()根据服务器机器的时区设置打印数据时间,除非您通过manage.py shell打开了python控制台。

Same goes for MySQL console. MySQL控制台也是如此。 It shows the datetime in your machine timezone rather than what's stored in database if I'm right. 如果我是对的,它会显示机器时区中的日期时间,而不是存储在数据库中的日期时间。

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

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