Last active
May 24, 2022 09:26
-
-
Save deangrant/cb51ed811da42ce14aa7069c73a687e6 to your computer and use it in GitHub Desktop.
A python function to convert a localized timestamp to epoch / unix time
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 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