Django:如何让 datetime 对象知道创建它的时区?
问题描述
我正在运行一个从远程服务器请求海潮数据的程序.此潮汐数据的 time
和 date
是根据我机器的本地时区计算的.我想使用这些本地日期和时间来创建一个 datetime
对象,然后将其保存在 Django 模型中.
datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)
如何确保 datetime
对象在将其发布到 Django 之前知道它是基于本地时区创建的?
我想,在发布之前我希望它看起来像这样:
datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute, loc_timezone)
如何动态获取机器的本地时区?以及如何确保所有用户都能看到转换为他们自己本地时区的时间.
解决方案首先,确保你熟悉 Django 的 时区文档,设置USE_TZ = True
,安装pytz
.
我不太明白你的约会是从哪里来的.如果它来自服务器作为他们数据的一部分(即它表示测量潮汐的时间),它应该已经是 UTC,或者您需要知道他们使用的时区.如果您正在创建它,那么最简单的方法就是使用 django.utils.timezone.now()
(返回时区感知日期时间).
如果您确实需要按照您的描述手动创建它,请按照用法 这里 或使用 make_aware()
:
从 django.utils.timezone 导入 make_aware天真 = 日期时间(loc_year,loc_month,loc_date,loc_hour,loc_minute)意识到 = make_aware(naive, timezone=pytz.timezone("Europe/Helsinki"))# 如果转换为当前时区,可以省略时区参数
current timezone 将是默认时区(由 TIME_ZONE
设置),除非你使用过 activate()
指定一个不同的.默认时区可能与服务器的系统时区相同,也可能不同.this answer 中讨论了以 pytz
可以理解的格式获取系统时区.p>
最后,如 这里:
<块引用>当前时区相当于翻译的当前语言环境.但是,Django 无法使用 Accept-Language HTTP 标头来自动确定用户的时区.相反,Django 提供了时区选择功能.使用它们来构建对您有意义的时区选择逻辑.
请参阅那里的示例以获得指导.
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. I want to use these local dates and times to create a datetime
object, which I will then save in a Django model.
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?
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
.
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.
If you really need to manually create it as you've described, follow the usage here or use make_aware()
:
from django.utils.timezone import make_aware
naive = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)
aware = make_aware(naive, timezone=pytz.timezone("Europe/Helsinki"))
# can leave off the timezone parameter if converting to the current 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. 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.
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.
See the examples there for guidance.
相关文章