Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
imoprt pytz
tz = pytz.utc
now = datetime.now(tz)
tz_us = pytz.timezone("US/Pacific")
now_us = datetime.now(tz_us)
JST = timezone(timedelta(hours=+9), 'JST')
now_jp = datetime.now(JST)
>>> now
datetime.datetime(2020, 9, 17, 12, 11, 37, 966557, tzinfo=<UTC>)
>>> now_us
datetime.datetime(2020, 9, 17, 5, 12, 5, 626677, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
>>> now_jp
datetime.datetime(2020, 9, 17, 21, 12, 38, 125351, tzinfo=datetime.timezone(datetime.timedelta(seconds=32400), 'JST'))
timestamp = int(now_us.timestamp())
>>> int(now_us.timestamp())
1600344725
>>> datetime.fromtimestamp(timestamp)
datetime.datetime(2020, 9, 17, 21, 12, 5)
>>> datetime.fromtimestamp(timestamp, tz=tz)
datetime.datetime(2020, 9, 17, 12, 12, 5, tzinfo=<UTC>)
>>> datetime.fromtimestamp(timestamp, tz=tz_us)
datetime.datetime(2020, 9, 17, 5, 12, 5, tzinfo=<DstTzInfo 'US/Pacific' PDT-1 day, 17:00:00 DST>)
>>> datetime.fromtimestamp(timestamp, tz=JST)
datetime.datetime(2020, 9, 17, 21, 12, 5, tzinfo=datetime.timezone(datetime.timedelta(seconds=32400), 'JST'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment