DEV Community

Fomalhaut Weisszwerg
Fomalhaut Weisszwerg

Posted on

[python] pytz, dateutil を使わずに異なるタイムゾーンの日時に変換する方法

TL;TR

datetime モジュールの astimezone メソッド を使います。

from datetime import datetime
from zoneinfo import ZoneInfo

lt = datetime.now(tz=ZoneInfo("localtime"))
lt.astimezone(ZoneInfo("US/Pacific"))
Enter fullscreen mode Exit fullscreen mode

Prerequisites

Windows ではシステムのタイムゾーンデータに直接アクセスできないので tzdata パッケージをインストールしておく必要があります。

また、ミニマムコンテナなど特殊な環境下でタイムゾーンデータが入っていない場合も同様に tzdata パッケージをインストールしておく必要があります。

Python が利用可能なタイムゾーンデータがあるかどうかを確認するには次のコマンドを実行します。

python3 -c '__import__("zoneinfo").available_timezones()'
Enter fullscreen mode Exit fullscreen mode

実行しても set() だけしか出力されない場合はタイムゾーンデータが入っていないということです。下記コマンドを実行して tzdata パッケージをインストールしてください。

pip install tzdata
Enter fullscreen mode Exit fullscreen mode

異なるタイムゾーンの日時への変換

次のステップで変換します

  1. タイムゾーン情報をもった datetime オブジェクトを作成する
  2. astimezone メソッドで目的のタイムゾーンの日時に変換する

それぞれについて詳しく説明します。

1. タイムゾーン情報をもった datetime オブジェクトを作成する

現在時刻を変換したいときは datetime.now メソッドの tz を指定します。変換元のタイムゾーンとして Python を実行している環境の設定を利用したい場合は ZoneInfo("localtime") を指定します。

from datetime import datetime
from zoneinfo import ZoneInfo

dt = datetime.now(tz=ZoneInfo("localtime"))
Enter fullscreen mode Exit fullscreen mode

datetime コンストラクタを直接呼び出すときは tzinfo に変換元のタイムゾーンを指定します。

from datetime import datetime
from zoneinfo import ZoneInfo

dt = datetime(2025, 1, 1, 0, 0, tzinfo=ZoneInfo("localtime"))
Enter fullscreen mode Exit fullscreen mode

データベースなどから取得した UNIX 時刻を変換したいときは fromtimestamp メソッドの tz を指定します。

from datetime import datetime
from zoneinfo import ZoneInfo

dt = datetime.fromtimestamp(unix_time, tz=ZoneInfo("UTC"))
Enter fullscreen mode Exit fullscreen mode
  1. astimezone メソッドで目的のタイムゾーンの日時に変換する

前ステップで作成した dt オブジェクトの astimezone メソッド に目的のタイムゾーンを指定して実行します。

dt.astimezone(ZoneInfo("US/Pacific"))
Enter fullscreen mode Exit fullscreen mode

トルコの 2025 年 1 月 1 日 0:00 を米国太平洋標準時 (PST) に変換すると下記のようになります。

from datetime import datetime
from zoneinfo import ZoneInfo

tr = datetime(2025, 1, 1, 0, 0, tzinfo=ZoneInfo("Europe/Istanbul"))
pst = tr.astimezone(ZoneInfo("US/Pacific"))

print(tr.isoformat())
print(pst.isoformat())
Enter fullscreen mode Exit fullscreen mode

実行結果

2025-01-01T00:00:00+03:00
2024-12-31T13:00:00-08:00
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Retry later