Python Django:如何让 datetime 对象知道创建它的时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22108634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:15:20  来源:igfitidea点击:

Django: How to make a datetime object aware of the timezone in which it was created?

pythondjangodatetimedjango-modelstimezone

提问by sgarza62

I am running a program that requests ocean tide data from a remote server. The timeand dateof this tide data is being computed based on my machine's local time zone. I want to use these local dates and times to create a datetimeobject, which I will then save in a Django model.

我正在运行一个从远程服务器请求海潮数据的程序。这个潮汐数据的timedate是根据我机器的本地时区计算的。我想使用这些本地日期和时间来创建一个datetime对象,然后将其保存在 Django 模型中。

datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)

How do I ensure that the datetimeobject is aware that it was created based on a local time zone, before posting it to Django?

在将datetime对象发布到 Django 之前,如何确保该对象知道它是基于本地时区创建的?

I guess, I would want it to look like this before posting it:

我想,在发布之前,我希望它看起来像这样:

datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute, loc_timezone)

How do I get the local timezone of my machine dynamically? And how to I ensure that all users see the time converted to their own local timezone.

如何动态获取机器的本地时区?以及如何确保所有用户看到时间转换为他们自己的本地时区。

采纳答案by Kevin Christopher Henry

First, make sure you're familiar with Django's documentation on timezones, set USE_TZ = True, and install pytz.

首先,确保您熟悉 Django关于 timezones、 setUSE_TZ = True和 install的文档pytz

I don't quite understand where your date is coming from. If it's coming from the server as part of their data (i.e. it represents when the tides were measured), it should either be in UTC already or you will need to know the time zone they are using. If you're creating it, then the simplest thing is just to use django.utils.timezone.now()(which returns a timezone-aware datetime) when you create your model instance.

我不太明白你的约会对象是从哪里来的。如果它作为数据的一部分来自服务器(即它代表测量潮汐的时间),则它应该已经是 UTC 或者您需要知道他们使用的时区。如果您正在创建它,那么最简单的方法就是django.utils.timezone.now()在创建模型实例时使用(它返回时区感知日期时间)。

If you really need to manually create it as you've described, follow the usage hereor use make_aware():

如果您确实需要按照您的描述手动创建它,请按照此处的用法或使用make_aware()

from django.utils.timezone import make_aware

naive = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)
make_aware(naive)  # current timezone, or...
make_aware(naive, timezone=pytz.timezone("Europe/Helsinki"))  # ...specific timezone

The current timezonewill be the default timezone (as defined by the TIME_ZONEsetting) unless you've used activate()to specify a different one. The default time zone may or may not be the same as the server's system timezone. Getting the system timezone in a format that pytzcan understand is discussed in this answer.

当前时区将成为默认的时区(由定义TIME_ZONE设置),除非你使用activate()指定不同的一个。默认时区可能与服务器的系统时区相同,也可能不同。pytz这个答案中讨论了以可以理解的格式获取系统时区。

Finally, ensuring that users see the time converted to their local time zone is non-trivial, as discussed here:

最后,确保用户看到的时间转换为当地时区是不平凡的,因为讨论在这里

The current time zone is the equivalent of the current locale for translations. However, there's no equivalent of the Accept-Language HTTP header that Django could use to determine the user's time zone automatically. Instead, Django provides time zone selection functions. Use them to build the time zone selection logic that makes sense for you.

当前时区相当于翻译的当前语言环境。但是,没有与 Django 可以用来自动确定用户时区的 Accept-Language HTTP 标头等效的标头。相反,Django 提供了时区选择功能。使用它们来构建对您有意义的时区选择逻辑。

See the examples there for guidance.

请参阅那里的示例以获取指导。

回答by HimalayanCoder

from django.utils import timezone
import pytz

timezone.activate(pytz.timezone("Asia/Kolkata"))
timezone.localtime(timezone.now())