How can I convert a date time string of the form Feb 25 2010, 16:19:20 CET
to the unix epoch?
Currently my best approach is to use time.strptime()
is this:
def to_unixepoch(s):
# ignore the time zone in strptime
a = s.split()
b = time.strptime(" ".join(a[:-1]) + " UTC", "%b %d %Y, %H:%M:%S %Z")
# this puts the time_tuple(UTC+TZ) to unixepoch(UTC+TZ+LOCALTIME)
c = int(time.mktime(b))
# UTC+TZ
c -= time.timezone
# UTC
c -= {"CET": 3600, "CEST": 2 * 3600}[a[-1]]
return c
I see from other questions that it might be possible to use calendar.timegm()
, and pytz
among others to simplify this, but these don't handle the abbreviated time zones.
I'd like a solution that requires minimal excess libraries, I like to keep to the standard library as much as possible.