django学习笔记:将数据库中的时间取出,转换为当地时间

setting设置

  • 在settings.py文件中将USE_TZ设置为False,将TIME_ZONE设置为亚洲上海,之后我们在模型中创建时间字段的时候,在数据库中存储的就是东八区的时间,而时间的类型会使navie类型,所以我们就不能再把navie类型的时间转换成其他时区的类型,所以我们一般不建议这么做。
  • Django在1.4版本之后存储如果设置了USE_TZ=True,则存储到数据库中的时间永远是UTC时间。这时如果settings里面设置了USE_TZ=True与TIME_ZONE = ‘UTC’,用datetime.datetime.now()获取的时间django会把这个时间当成UTC时间存储到数据库中去。如果修改设置为USE_TZ=True与TIME_ZONE = ‘Asia/Shanghai’,用datetime.datetime.now()获取的时间由于不带时区,django会把这个时间当成Asia/Shanghai时间,即东八区时间,然后django会把这个时间转成带时区UTC时间存储到数据库中去,而读的时候直接按UTC时间读出来,这就是网上很多人遇到的存储到数据库中的时间比本地时间会小8个小时的原因。

时间的使用

  • from django.utils.timezone import localtime #将数据库中的时间转换为当地时间
  • 时间=localtime(数据库中取出的时间)

get_fixed_timezone(offset)¶
返回一个 tzinfo 实例,该实例表示一个与 UTC 有固定偏移的时区。

offset 是一个 datetime.timedelta 或一个整数分钟。UTC 以东的时区为正值,UTC 以西的时区为负值。

get_default_timezone()¶
返回一个 tzinfo 实例,表示 默认时区。

get_default_timezone_name()¶
返回 默认时区 的名称。

get_current_timezone()¶
返回一个 tzinfo 实例,表示 当前时区。

get_current_timezone_name()¶
返回 当前时区 的名称。

activate(timezone)¶
设置 当前时区。timezone 参数必须是 tzinfo 子类的实例或时区名称。

deactivate()¶
取消设置 当前时区。

override(timezone)¶
这是一个 Python 上下文管理器,它在进入时通过 activate() 设置 当前时区,并在退出时恢复之前的活动时区。如果 timezone 参数是 None,则 当前时区 在进入时用 deactivate() 取消设置。

override 也可作为函数装饰器使用。

localtime(value=None, timezone=None)¶
将一个感知的 datetime 转换为不同的时区,默认为 当前时区。

当省略 value 时,默认为 now()。

这个函数不适用于 naive 的日期,请使用 make_aware() 代替。

localdate(value=None, timezone=None)¶
使用 localtime() 将一个已知道的 datetime 转换为不同时区的 date(),默认为 当前时区。

当省略 value 时,默认为 now()。

此功能不适用于 naive 的日期。

now()¶
返回一个 datetime,表示当前的时间点。具体返回什么取决于 USE_TZ 的值。

如果 USE_TZ 是 False,这将是一个 naive 的日期时间(即一个没有关联时区的日期时间),代表系统当地时区的当前时间。
如果 USE_TZ 是 True,这将是一个 aware 的日期时间,代表当前的 UTC 时间。请注意,无论 TIME_ZONE 的值是多少, now() 将始终返回以 UTC 为单位的时间;你可以使用 localtime() 来获取当前时区的时间。
is_aware(value)¶
如果 value 是感知的,返回 True,如果是 naive 的,则返回 False。本函数假设 value 是一个 datetime。

is_naive(value)¶
如果 value 是 naive 的,返回 True,如果是感知的,则返回 False。本函数假设 value 是一个 datetime。

make_aware(value, timezone=None, is_dst=None)¶
返回一个感知的 datetime,在 timezone 中表示与 value 相同的时间点,value 是一个 naive 的 datetime。如果 timezone 设置为 None,则默认为 当前时区。

4.0 版后已移除:
When using pytz, the pytz.AmbiguousTimeError exception is raised if you try to make value aware during a DST transition where the same time occurs twice (when reverting from DST). Setting is_dst to True or False will avoid the exception by choosing if the time is pre-transition or post-transition respectively.

When using pytz, the pytz.NonExistentTimeError exception is raised if you try to make value aware during a DST transition such that the time never occurred. For example, if the 2:00 hour is skipped during a DST transition, trying to make 2:30 aware in that time zone will raise an exception. To avoid that you can use is_dst to specify how make_aware() should interpret such a nonexistent time. If is_dst=True then the above time would be interpreted as 2:30 DST time (equivalent to 1:30 local time). Conversely, if is_dst=False the time would be interpreted as 2:30 standard time (equivalent to 3:30 local time).

is_dst 参数已被废弃,将在 Django 5.0 中删除。

make_naive(value, timezone=None)¶
返回一个 naive 的 datetime,它在 timezone 中表示与 value 相同的时间点,value 是一个感知的 datetime。如果 timezone 设置为 None,则默认为 当前时区。

Original: https://blog.csdn.net/qq_42202384/article/details/122905737
Author: 墨無
Title: django学习笔记:将数据库中的时间取出,转换为当地时间

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/734510/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球