Last active
May 8, 2021 19:57
-
-
Save kkAyataka/6c336863c7446e8766b2 to your computer and use it in GitHub Desktop.
Python timezone conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""timezoneの変換""" | |
import datetime | |
class UTC(datetime.tzinfo): | |
def utcoffset(self, dt): | |
return datetime.timedelta() | |
def dst(self, dt): | |
return datetime.timedelta() | |
class JST(datetime.tzinfo): | |
def utcoffset(self, dt): | |
return datetime.timedelta(hours=+9) | |
def dst(self, dt): | |
return datetime.timedelta() | |
d = datetime.datetime(2014, 1, 2, 9, 30, 5, tzinfo=JST()) | |
print d.isoformat() # 2014-01-02T09:30:05+09:00 | |
# astimezoneはtzinfoに従って時刻を変換する | |
d = d.astimezone(UTC()) | |
print d.isoformat() # 2014-01-02T00:30:05+00:00 | |
# replaceはtzinfoの単純置き換えで時刻は変換されない | |
d = d.replace(tzinfo=JST()) | |
print d.isoformat() # 2014-01-02T00:30:05+09:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment