Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Last active September 17, 2020 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/4b5034f0414d164d652b885467bee95c to your computer and use it in GitHub Desktop.
Save yuyasugano/4b5034f0414d164d652b885467bee95c to your computer and use it in GitHub Desktop.
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