Skip to content

Instantly share code, notes, and snippets.

@deangrant
Last active May 24, 2022 09:26
Show Gist options
  • Save deangrant/cb51ed811da42ce14aa7069c73a687e6 to your computer and use it in GitHub Desktop.
Save deangrant/cb51ed811da42ce14aa7069c73a687e6 to your computer and use it in GitHub Desktop.
A python function to convert a localized timestamp to epoch / unix time
from dateutil import parser
from pytz import timezone
from datetime import datetime
import time
def localized_time_to_epoch(timestamp, time_zone):
strptime = "%Y-%m-%d %H:%M:%S"
timestamp = parser.parse(timestamp)
localize = timezone(timezone).localize(datetime.strptime(str(timestamp), strptime))
utc = localize.astimezone(timezone('UTC')).strftime(strptime)
return int(time.mktime(time.strptime(utc, strptime)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment