Python / Django MySQL日期时间处理和时区 [英] Python/Django MySQL Datetime handling and timezone

查看:1033
本文介绍了Python / Django MySQL日期时间处理和时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为的表单,其字段为 date

  date = models.DateTimeField(auto_now_add = True)

当我添加一个元素时,MySQL中的日期是UTC日期,但是我住在UTC + 2时区



我想我正确地设置时区 settings.py

  TIME_ZONE ='欧洲/巴黎'

Python使用正确的时区:

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

MySQL也是:

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

我可以设置日期属性manualy,它的工作,但我想知道为什么auto_now_add返回错误的日期虽然python和mysql使用正确的时区



谢谢

解决方案

这是一个复杂的绑定解释。从 Django 1.4



< blockquote>

当USE_TZ为False时,是Django将存储
所有数据时间的时区。当USE_TZ为True时,
Django将用于在模板中显示数据时间并解释
数据表输入的默认时区。


是指 TIME_ZONE 。那么你的 USE_TZ 是什么?如果您的 USE_TZ 为True,那么Django将以UTC形式存储datetime,并使用 TIME_ZONE 显示在模板中并解释表单。 / p>

这是因为,如果您稍后在另一个地区托管您的网站时更改了 TIME_ZONE ,则可以轻松转换任何从UTC到任何时区的数据时间。



Django 1.3 及更早版本


请注意,是时区Django将转换所有
日期/时间 - 不一定是服务器的时区。例如,
一个服务器可能会为多个Django供电的站点提供服务,每个站点都有
独立的时区设置。



通常,Django设置os.environ ['TZ']变量指定到TIME_ZONE设置中指定的时区
。因此,您的所有观点和模型
将在正确的时区自动运行。


但不告诉你datetime将存储在数据库中的时区。需要尝试(我的猜测是UTC)。



print datetime.datetime.now()打印数据时间根据您的服务器机器的时区设置,除非您通过 manage.py shell 打开了python控制台。



MySQL控制台也是如此。它显示机器时区中的日期时间,而不是数据库中存储的日期时间(如果我是正确的)。


I've got a model called Vote with a field 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

I think I correctly set the timezone in settings.py :

TIME_ZONE = 'Europe/Paris'

Python use the right timezone :

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

MySQL too :

> 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

Thank you

解决方案

It's a complex binding to explain. From Django 1.4,

When USE_TZ is False, this is the time zone in which Django will store all datetimes. 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.

this refers to TIME_ZONE. So what is your 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.

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.

In Django 1.3 and earlier,

Note that this is the time zone to which Django will convert all dates/times – not necessarily the timezone of the server. For example, one server may serve multiple Django-powered sites, each with a separate time-zone setting.

Normally, Django sets the os.environ['TZ'] variable to the time zone you specify in the TIME_ZONE setting. 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. Need to experiment anyway(my guess is 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.

Same goes for MySQL console. It shows the datetime in your machine timezone rather than what's stored in database if I'm right.

这篇关于Python / Django MySQL日期时间处理和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆