Home > Backend Development > Python Tutorial > [Python] How to convert different time zones without using pytz, dateutil

[Python] How to convert different time zones without using pytz, dateutil

Mary-Kate Olsen
Release: 2025-01-27 16:11:13
Original
457 people have browsed it

No need to Pytz and Datemil, Python's native implementation of the dated time conversion of different time zones

This article introduces how to use the datetime module and zoneinfo module in Python 3.6 and above versions. Without a third -party library pytz and dateutil, you can easily realize the conversion of the date time of different time zones.

Core method: Use the method of the datetime object. astimezone

Brief step:

Create the
    object containing the time zone information.
  1. datetime Use the method to convert the date time into the target time zone.
  2. astimezone Prerequisites:

python & gt; = 3.6

TZData (Windows system and certain special environments may need to be installed)
  • Check and install tzdata:
Run the following commands in the terminal or command prompt to check whether the necessary time zone data is installed in the system:

If the output is an empty set

, you need to install

:

<code class="language-bash">python3 -c '__import__("zoneinfo").available_timezones()'</code>
Copy after login

Detailed steps and examples: set() tzdata

<.> 1. Create
<code class="language-bash">pip install tzdata</code>
Copy after login
objects containing the time zone information:

Get the current time and specify the time zone:

Use the method, and specify the datetime parameter as to get local time.

  • Specify a specific date of time and time zone: datetime.now() Use the tz construct function, and specify the ZoneInfo("localtime") parameter.
<code class="language-python">from datetime import datetime
from zoneinfo import ZoneInfo

dt = datetime.now(tz=ZoneInfo("localtime"))</code>
Copy after login
  • Convert from UNIX timestamp: Use the method, and specify the datetime parameter. tzinfo
<code class="language-python">dt = datetime(2025, 1, 1, 0, 0, tzinfo=ZoneInfo("Europe/Istanbul"))</code>
Copy after login
<.> 2. Use
    Method conversion time zone:
  • Use the method to convert datetime.fromtimestamp() objects into target time zones. tz
Example:
<code class="language-python">unix_time = 1675000000  # 示例UNIX时间戳
dt = datetime.fromtimestamp(unix_time, tz=ZoneInfo("UTC"))</code>
Copy after login
Converting Turkish Istanbul time on January 1, 2025 to 0:00 to Pacific Standard Time (PST).

astimezone The output results are similar:

astimezone() datetime Through the above steps, you can easily use the Python native module to complete the conversion of the dated time in different time zones without relying on the additional third -party library. Remember to choose the appropriate method of creating the

object according to your actual needs.
<code class="language-python">pst = dt.astimezone(ZoneInfo("US/Pacific"))</code>
Copy after login

The above is the detailed content of [Python] How to convert different time zones without using pytz, dateutil. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template