简体   繁体   English

Django和时区

[英]Django and time zone

Django 1.7, PostgreSQL. Django 1.7,PostgreSQL。

I want to store datetime in UTC and display it in PST time zone. 我想以UTC格式存储日期时间并将其显示在PST时区中。

My local time: 8:05 am 我当地时间:上午8:05

UTC time: 1:05 am UTC时间:凌晨1点05分

PST time: 6:05 pm PST时间:下午6:05

Django doc: Django doc:

When support for time zones is enabled, Django stores date and time 
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.

setting.py setting.py

USE_TZ = True
TIME_ZONE = 'US/Pacific'

Model field 模型领域

created_at = models.DateTimeField(auto_now_add=True)

I have created new model, and in django admin it display PST time (6:05 pm). 我创建了新模型,在django admin中显示PST时间(下午6:05)。 It is OK. 没关系。 But if I do: 但如果我这样做:

select created_at from my_table where id = 1;

It display my local time (8:05 am)! 它显示我当地时间(上午8:05)! So, I'm not sure that this was stored in UTC time. 所以,我不确定这是存储在UTC时间。

And one more thing. 还有一件事。

Usual datetime field, I have set in admin this date: 2014-10-25 18:00:00 通常的日期时间字段,我在管理员这个日期设置:2014-10-25 18:00:00

Id displayed in admin Oct. 25 , 2014, 6 pm ID于2014年10月25日下午6点在管理员中显示

But select from DB show me: 但是从DB中选择让我看看:

2014-10- 26 08:00:00.0 2014-10- 26 08:00:00.0

So, I definitely do not understand what's going on. 所以,我绝对不明白发生了什么。 Where is my mistake? 我的错误在哪里?

Basically what happens is that time is stored in database using the timestamp, but displays the time using timezone specified in your database, which is unless manually changed is the timezone of the machine. 基本上发生的情况是时间使用时间戳存储在数据库中,但使用数据库中指定的时区显示时间,除非手动更改是计算机的时区。 But since you specify a different timezone in django, django will adjust the difference. 但是由于你在django中指定了一个不同的时区,django会调整差异。 So what you need to do is change the timezone in db to UTC (process differs depending on the engine) 所以你需要做的是将db中的时区更改为UTC(进程因引擎而异)

The way I prefer to do it is leaving database timezone unchanged, specify 'UTC' in django settings, and then whenever displaying time to user, using some javascript convert it to local users time. 我喜欢这样做的方法是保持数据库时区不变,在django设置中指定'UTC',然后每当向用户显示时间时,使用一些javascript将其转换为本地用户时间。

Edit 编辑

Didn't notice before that you are using PostgreSQL. 之前没有注意到你正在使用PostgreSQL。 I think you can just change timezone in postgresql.conf or change the TimeZone variable to UTC in database 我想你可以在postgresql.conf中更改时区或在数据库中将TimeZone变量更改为UTC

I think Alexey Kuleshevich answers the first part of your question: When you do the manual select , PostgreSQL is displaying the timestamp in the timezone configured on the database. 我认为Alexey Kuleshevich回答了你问题的第一部分:当你进行手动select ,PostgreSQL会在数据库中配置的时区中显示时间戳。 That doesn't affect Django, though, which is behaving according to the documentation. 但是,这并不影响Django,它根据文档表现出来。

As for the second part of your question: When you enter a datetime value in a form, Django interprets it in the current time zone , which by default is the same as your TIME_ZONE setting. 至于问题的第二部分:当您在表单中输入日期时间值时,Django会在当前时区中对其进行解释,默认情况下该时区TIME_ZONE设置相同。 So when you enter 6pm in the admin, Django interprets that as being 6pm PST, and stores it as 1am UTC (the next day). 因此,当您在管理员中输入下午6点时,Django会将其解释为太平洋标准时间下午6点,并将其存储为UTC时间凌晨1点(第二天)。 The database is then displaying that as 8am local time (the next day). 然后数据库显示为当地时间早上8点(第二天)。

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

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