0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonでepochにタイムゾーン情報をつける

Last updated at Posted at 2018-09-01

epoch->UTC->JSTの順で変換します。

手順

timezoneconvert.py
import pytz
from datetime import datetime
# 定数を定義(1535763600 = September 1, 2018 1:00:00 AM UTC)
EPOCH = 1535763600
# timezoneを定義
UTC = pytz.timezone('UTC')
JST = pytz.timezone('Asia/Tokyo')
# epoch timeの読み込み
utc_time_naive = datetime.utcfromtimestamp(EPOCH)
# epoch timeにUTCのタイムゾーン情報を付与
utc_time_aware = UTC.localize(utc_time_naive)
# JSTのタイムゾーンへ変換
jst_time_aware = utc_time_aware.astimezone(JST)

Note

  • 利用する環境がJSTであれば
    jst_time_naive = datetime.fromtimestamp(EPOCH)
    とすればJSTのnaive timeが取得できます。

  • timezoneを'Asia/Tokyo'にしても、下記のように時間が19分ずれる場合があるので注意してください

>>> jp_old=datetime(1887, 9, 1, 1, 0)
>>> JP.localize(jp_old)
datetime.datetime(1887, 9, 1, 1, 0, tzinfo=<DstTzInfo 'Asia/Tokyo' LMT+9:19:00 STD>)
  • 他地域のtimezone名称に関してはこちらを参考にしてください。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?