Django:如何使datetime对象知道创建它的时区? [英] Django: How to make a datetime object aware of the timezone in which it was created?

查看:151
本文介绍了Django:如何使datetime对象知道创建它的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个从远程服务器请求海潮数据的程序。该潮汐数据的时间日期是根据我的机器的本地时区计算的。我想使用这些本地日期和时间来创建一个 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() (返回时区感知日期时间)。



如果您真的需要手动创建它,如您所述,请按照使用方法这里

  import pytz 
datetime_obj = datetime(loc_year,loc_month,loc_date,loc_hour,loc_minute)
pytz.timezone(Europe / Helsinki)。localize(datetime_obj)

而不是欧洲/赫尔辛基,您可以使用 django.utils.timezone.get_default_timezone_name() 。这将是您使用 TIME_ZONE 设置,可能不是实际的系统时区。以此答案 c c>可以理解的格式获取系统时区, / a>。



最后,确保用户看到转换到其本地时区的时间是不重要的,如


$ b $($)$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b


当前时区相当于当前的翻译区域。但是,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:

import pytz
datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)
pytz.timezone("Europe/Helsinki").localize(datetime_obj)

Instead of "Europe/Helsinki" you can get the default time zone with django.utils.timezone.get_default_timezone_name(). This will be the time zone you defined with the TIME_ZONE setting, and may not be the actual system time zone. 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.

这篇关于Django:如何使datetime对象知道创建它的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆