Python 3 标准库中的 datetime.timezone class 有实际用途吗?

Is there any practical use for the datetime.timezone class in Python 3 standard library?

datetime.timezone class 的文档只说:

A class that implements the tzinfo abstract base class as a fixed offset from the UTC.

它接受时间增量作为参数。

我从未见过直接从其他人的代码片段中使用它的示例,尽管我相信它一定有一些用途,否则 Python 没有必要暴露这个 API。那么在什么情况下会建议直接使用这个class呢?与使用专用库(例如 pytz)相比,这有什么优势?

来自Python 3 documentation

The datetime module supplies a simple concrete subclass of tzinfo, timezone, which can represent timezones with fixed offset from UTC such as UTC itself or North American EST and EDT.

基本思想是,对于只是 UTC 时间偏移量的时区(即 UTC +/- 一些固定的分钟数),实现 tzinfo 对象所需的所有方法比必要的要多得多,因此您可以只需使用偏移值对时区对象进行子类化。

文档本身也推荐使用 pytz 处理时区:

pytz library brings the IANA timezone database (also known as the Olson database) to Python and its usage is recommended

datetime docs python 3.6+ 的更新是不再使用 pytz:

dateutil.tz library brings the IANA timezone database (also known as the Olson database) to Python and its usage is recommended.

建议使用

dateutil.tz 而不是 pytz,因为如果时区由于夏令时或其他原因而有多个偏移量,pytz 会以意想不到的方式处理 datetime.timedelta 操作。 This article explains in more detail.