« Back to Index

[Python timestamp to epoch and back, inc. UTC timezone]

View original Gist on GitHub

Tags: #python #timezone #timestamp #epoch #utc

Python timestamp to epoch and back.py

from datetime import datetime, timedelta, timezone

datetime(2018,3,30,23,30).timestamp()
# 1522449000.0

datetime.fromtimestamp(1522449000).strftime('%c')
# 'Fri Mar 30 23:30:00 2018'

n = datetime.now()
t = n - timedelta(minutes=15)
# datetime.datetime(2019, 10, 15, 11, 11, 6, 149052)
t.timestamp()
# 1571134266.149052

utc_timestamp = datetime(2020,6,6,13,00, tzinfo=timezone.utc).timestamp()
# 1591448400.0
datetime.fromtimestamp(utc_timestamp, tz=timezone.utc)
# datetime.datetime(2020, 6, 6, 13, 0, tzinfo=datetime.timezone.utc)