Last active
September 17, 2020 12:02
-
-
Save yuyasugano/4b5034f0414d164d652b885467bee95c to your computer and use it in GitHub Desktop.
This file contains 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
from datetime import datetime, timezone, timedelta | |
import calendar | |
import time | |
now = datetime.now() | |
utc = datetime.utcnow() | |
>>> now | |
datetime.datetime(2020, 9, 17, 20, 52, 15, 31149) | |
>>> utc | |
datetime.datetime(2020, 9, 17, 11, 52, 15, 215760) | |
# Before Python3.3 need to use time module or timestamp function after Python3.3 | |
timestamp = int(time.mktime(now.timetuple())) | |
>>> timestamp | |
1600343535 | |
>>> int(now.timestamp()) | |
1600343535 | |
timestamp_utc = calendar.timegm(utc.utctimetuple()) | |
>>> timestamp_utc | |
1600343535 | |
loc = datetime.fromtimestamp(timestamp) | |
utc = datetime.utcfromtimestamp(timestamp_utc) | |
>>> loc | |
datetime.datetime(2020, 9, 17, 20, 52, 15) | |
>>> utc | |
datetime.datetime(2020, 9, 17, 11, 52, 15) | |
>>> int(time.mktime(utc.timetuple())) | |
1600311135 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment