Created
April 30, 2018 03:55
-
-
Save dogrdon/36e83db46a0ccf1b3aef4bfade294724 to your computer and use it in GitHub Desktop.
Convert a date time string with a specified format and timezone to Unix Epoch time stamp
This file contains hidden or 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
# timezone solution from: https://stackoverflow.com/questions/12165691/python-datetime-with-timezone-to-epoch/17257177#17257177 | |
''' | |
Take a timezone in the form of `MM/DD/YYYY HH:MM:SS {AM/PM}` | |
and convert to unix epoch in python3 | |
''' | |
from datetime import datetime | |
import pytz | |
orig = "4/29/2018 11:27:00 PM" | |
def format_date(dt): | |
d = dt.split()[0] | |
t = dt.split()[1:] | |
MM, DD, YYYY = d.split('/') | |
HH, MIN, SS = t[0].split(':') | |
if t[1] == 'PM': | |
HH = (int(HH) + 12) | |
dtobj = datetime(int(YYYY), int(MM), int(DD), int(HH), int(MIN), int(SS)) | |
return dtobj | |
def convert_timestamp(dateIN): | |
tz = pytz.timezone('US/Eastern') | |
dt_to_convert = format_date(dateIN) | |
# a datetime with timezone | |
dt_with_tz = tz.localize(dt_to_convert, is_dst=None) | |
# get timestamp | |
ts = (dt_with_tz - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds() | |
return ts | |
convert_timestamp(orig) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment