简体   繁体   English

Django时区UTC

[英]Django Timezone UTC

I am currently working on Django project with a postgres DB. 我目前正在使用postgres DB进行Django项目。 The data stored in db with time-stamp using Naive time (user's local time). 使用朴素时间(用户的本地时间)的时间戳存储在db中的数据。 However, in the setting.py, we have 但是,在setting.py中,我们有

USE_TZ = True

which means all the time-stamps retrieved by Django ORM are converted to UTC. 这意味着Django ORM检索到的所有时间戳都将转换为UTC。

Generally, it is ok. 一般来说,没关系。 However, for the function I am building right now does need the real time (uesr's local time). 但是,对于我现在正在构建的功能,确实需要实时(uesr的本地时间)。 Certainly, I would fetch the data and convert the time to whatever time I want, with two problems: 1. I can convert time-stamps to EST or anything, but I still don't know the original time; 当然,我会获取数据并将时间转换为所需的任何时间,这有两个问题:1.我可以将时间戳转换为EST或其他任何格式,但我仍然不知道原始时间; 2. I want to do the converting during the ORM query rather than after, since it will be more efficient. 2.我想在ORM查询期间而不是之后进行转换,因为这样会更高效。

Does anyone have a clue about this? 有人对此有任何线索吗? Thanks in advance! 提前致谢!

1) If the original datetimes really are naive then I would assume they got stored as whatever timezone you have set in your TIME_ZONE setting (defaults to 'America/Chicago' but might be something else in your case). 1)如果原始日期时间真的很幼稚,那么我认为它们会存储为您在TIME_ZONE设置中设置的任何时区(默认为'America/Chicago'但在您的情况下可能是其他时间)。 So converting back to that timezone will probably give you the original time. 因此,转换回该时区可能会给您原始时间。 From the django docs: 从Django文档中:

"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." “当USE_TZ为False时,这是Django将存储所有日期时间的时区。当USE_TZ为True时,这是Django将在模板中显示日期时间并解释在表单中输入的日期时间的默认时区。”

( https://docs.djangoproject.com/en/1.6/ref/settings/#std:setting-TIME_ZONE ) https://docs.djangoproject.com/zh-CN/1.6/ref/settings/#std:setting-TIME_ZONE

Normally the timezone information is actually set on the db connection ( https://docs.djangoproject.com/en/dev/ref/databases/#optimizing-postgresql-s-configuration ), so it's expected that you don't get your datetime in UTC when connecting to postgres through psql since the timezone used will default to your system's timezone, I'm not sure why this doesn't happen with raw queries in django though. 通常,时区信息实际上是在db连接上设置的( https://docs.djangoproject.com/en/dev/ref/databases/#optimizing-postgresql-s-configuration ),因此,预计您不会获得通过psql连接到postgres时,使用UTC的datetime,因为使用的时区将默认为系统的时区,但我不确定为什么django中的原始查询不会发生这种情况。

2) I can't say I've done many hours of research, however I believe that setting USE_TZ kind of dooms you to have to convert post-query. 2)我不能说我已经完成了许多小时的研究,但是我相信设置USE_TZ您不得不转换后查询。 You can possibly override the connection timezone, but I don't know of an easy way to do this at runtime since it will default to UTC due to USE_TZ . 您可以覆盖连接时区,但是我不知道在运行时执行此操作的简单方法,因为由于USE_TZ它会默认为UTC。

On previous projects I've worked on and ran into similar issues we have either passed the responsibility over to the front-end as someone suggested in your comments(frontend returns UTC datetimes and converts data back from UTC) or we also store the user timezone and do a post-query conversion. 在我之前从事过的项目中,遇到了类似的问题,我们要么按照您的意见中的建议将责任转移给了前端(前端返回UTC日期时间并将数据从UTC转换回),或者我们还存储了用户时区并进行查询后转换。 This didn't prove to be inefficient in our use case. 在我们的用例中,这并未证明效率低下。

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

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