SoFunction
Updated on 2025-03-01

Explore examples of time zone operation processing in Python in zoneinfo module

In Python 3.9 and later, the zoneinfo module was introduced

In Python 3.9 and later, the introduction ofzoneinfoModule for handling time zone-related operations. This module provides a simple and intuitive way to process time zone information, making it easier to perform time zone conversion and time zone-aware date-time processing in Python. This article will introducezoneinfoBasic usage of modules and provide sample code and parsing.

Install

zoneinfoModules are part of the standard library for Python 3.9 and later, so they can be used without additional installation.

Example

Here are a few examples that showzoneinfoUsage of modules:

Example 1: Get time zone information

Sample code:

from zoneinfo import ZoneInfo

# Get New York time zone informationny_tz = ZoneInfo("America/New_York")
print(ny_tz)

Output:

America/New_York

Analysis

In the above example, byZoneInfoThe class creates any_tzObject, represents the New York time zone.ZoneInfoThe parameter is a valid time zone name. The output results show the name of the time zone.

Example 2: Time Zone Conversion

Sample code:

from datetime import datetime
from zoneinfo import ZoneInfo

# Create a date and time objectdt = datetime(2022, 1, 1, 12, 0, 0)

# Convert datetime objects to New York time zoneny_tz = ZoneInfo("America/New_York")
ny_dt = (ny_tz)
print(ny_dt)

Output:

2022-01-01 12:00:00-05:00

Analysis:

In the above example, a date time object is first createddt, indicating 12:00:00 on January 1, 2022. Then, throughastimezoneMethod converts datetime objects to New York time zone (America/New_York). The output results show the converted date and time object, including time zone information.

Example 3: Get the system default time zone

Sample code:

from zoneinfo import ZoneInfo

# Get the default time zone of the systemdefault_tz = ()
print(default_tz)

Output:

Asia/Shanghai

Analysis:

In the above example, by()The method obtains the system default time zone information. The output results show the name of the system's default time zone.

Example 4: Traversing time zone information

Sample code:

from zoneinfo import available_timezones

# traverse all available time zonesfor tz in available_timezones():
   print(tz)

Output:

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
...

Analysis:

In the above example, useavailable_timezones()The function takes all available time zone information and outputs the name of each time zone through loop traversal.

Summarize

zoneinfoThe module provides a convenient way to handle time zone-related operations in Python. Through this module, we can easily obtain time zone information, perform time zone conversion and time zone-aware date and time-zone-aware date and time-zone processing. This article introduceszoneinfoBasic usage of modules and provide sample code and parsing. I hope this article will be understood and used by youzoneinfoModules help!

The above is the detailed content of exploring the usage examples of the zoneinfo module in Python. For more information about the usage of the zoneinfo module in Python, please follow my other related articles!