Python: ローカルタイムを別のタイムゾーンに変換する 質問する

Python: ローカルタイムを別のタイムゾーンに変換する 質問する

Python で現在の時刻を +0900 に変換したいです。

これを行う適切な方法は何ですか (時間モジュール内と仮定)?

これは Python には含まれていないので、pytz のようなものを使用する必要があると読みました。

サーバー全体またはグローバルに変更するのではなく、この 1 つのインスタンスだけを変更したいのです。

ベストアンサー1

UTC から IST へのデータ変換の例

from datetime import datetime
from pytz import timezone

format = "%Y-%m-%d %H:%M:%S %Z%z"

# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print now_utc.strftime(format)
Output: 2015-05-18 10:02:47 UTC+0000

# Convert to Asia/Kolkata time zone
now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
print now_asia.strftime(format)
Output: 2015-05-18 15:32:47 IST+0530

おすすめ記事