简体   繁体   English

关于Python中的pytz模块和time模块

[英]Regarding the pytz module and the time module in Python

I am a beginner in Python and trying to understand about working with dates and times.我是 Python 的初学者,并试图了解如何处理日期和时间。 Could you help me understanding the following concept?你能帮我理解以下概念吗?

In time module, we have the ability to get the local time zone by doing the following,在 time 模块中,我们可以通过执行以下操作来获取本地时区,

import time

if time.daylight:
    local_timezone_name = time.tzname[0]
    utc_offset = time.timezone
else:
    local_timezone_name = time.tzname[1]
    utc_offset = time.altzone

So what I want to clarify are,所以我想澄清的是,

  1. Isn't it suffice to just use the time module as above when dealing with local time zones instead of using pytz module?在处理本地时区时,仅使用上面的 time 模块而不是使用pytz模块就足够了吗? If not why pytz module is necessary?如果不是,为什么需要 pytz 模块?
  2. What advantages do one gain by using pytz module when dealing with dates and times in Python?在 Python 中处理日期和时间时,使用 pytz 模块有什么好处? (I don't understand how it differs from what Python already has built into it and how it handles dates and times efficently?) (我不明白它是什么的Python已经有了内置到它不同之以及如何处理日期和时间efficently?)

Thank you.谢谢你。

A long story.一个很长的故事。

Note: pytz is now obsolete (since two weeks), so with Python 3.9 you can uses directly standard libraries.注意:pytz 现在已经过时了(两周后),所以在 Python 3.9 中你可以直接使用标准库。

The problem: pytz is updated independently from Python releases, so it could change quickly the timezone changes.问题:pytz 的更新独立于 Python 版本,因此它可能会随着时区的变化而迅速改变。 It is not so seldom that within a week or so a country decide to change daylight time implementation.一个国家在一周左右的时间内决定改变夏令时实施的情况并不少见。 This would cause a lot of problem using standard Python, were you do not update so often (and it will requires a lot of burdens, just to update one line in timezone file).如果您不经常更新,这会导致使用标准 Python 出现很多问题(这将需要很多负担,只需更新时区文件中的一行)。 So Python didn't include timezone information, but for UTC (but it could handle time offsets and daylight flag), and some information about your local timezone (and ev. daylight).因此,Python 不包含时区信息,而是包含 UTC(但它可以处理时间偏移和日光标志),以及有关本地时区(和 ev. 日光)的一些信息。

Now it seems they found a solution, but Python 3.9 is very new (it was released two weeks ago).现在他们似乎找到了解决方案,但 Python 3.9 是非常新的(两周前发布)。

So, if you do not have python 3.9, pytz is the way to go.所以,如果你没有 python 3.9, pytz 是要走的路。 You have updated timezones and daylight periods.您已更新时区和夏令时。

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

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