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.
The Python standard library does not really implement time zones. You should use
python-dateutil. It provides useful extensions to the standarddatetimemodule including a time zones implementation and a parser.You can convert time zone aware
datetimeobjects to UTC with.astimezone(dateutil.tz.tzutc()). For the current time as a timezone aware datetime object, you can usedatetime.datetime.utcnow().replace(tzinfo=dateutil.tz.tzutc()).Unfortunately this technique will be wrong during the repeated daylight savings time hour.