简体   繁体   English

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

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

I am running a program that requests ocean tide data from a remote server. 我正在运行一个从远程服务器请求海潮数据的程序。 The time and date of this tide data is being computed based on my machine's local time zone. 此潮汐数据的timedate是根据我的机器的当地时区计算的。 I want to use these local dates and times to create a datetime object, which I will then save in a Django model. 我想使用这些本地日期和时间来创建一个datetime对象,然后我将保存在Django模型中。

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

How do I ensure that the datetime object is aware that it was created based on a local time zone, before posting it to Django? 在将datetime对象发布到Django之前,如何确保datetime对象知道它是基于本地时区创建的?

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. 以及如何确保所有用户都将时间转换为他们自己的本地时区。

First, make sure you're familiar with Django's documentation on timezones , set USE_TZ = True , and install pytz . 首先,确保您熟悉Django 关于时区文档 ,设置USE_TZ = True ,然后安装pytz

I don't quite understand where your date is coming from. 我不太明白你的约会对象来自哪里。 If it's coming from the server as part of their data (ie 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. 如果它来自服务器作为其数据的一部分(即它表示测量潮汐时),它应该已经是UTC,或者您需要知道它们正在使用的时区。 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. 如果您正在创建它,那么最简单的方法就是在创建模型实例时使用django.utils.timezone.now() (它返回一个时区感知的日期时间)。

If you really need to manually create it as you've described, follow the usage here or 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 timezone will be the default timezone (as defined by the TIME_ZONE setting) unless you've used activate() to specify a different one. 除非您使用activate()指定其他时区,否则当前时区将是默认时区(由TIME_ZONE设置定义)。 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 pytz can understand is discussed in this answer . 这个答案中讨论了以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. 但是,没有相当于Django可以用来自动确定用户时区的Accept-Language HTTP标头。 Instead, Django provides time zone selection functions. 相反,Django提供时区选择功能。 Use them to build the time zone selection logic that makes sense for you. 使用它们来构建对您有意义的时区选择逻辑。

See the examples there for guidance. 请参阅那里的示例以获得指导。

from django.utils import timezone
import pytz

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

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

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