This question already has answers here:
Closed 5 years ago.
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 standard datetime
module including a time zones implementation and a parser.
You can convert time zone aware datetime
objects to UTC with .astimezone(dateutil.tz.tzutc())
. For the current time as a timezone aware datetime object, you can use datetime.datetime.utcnow().replace(tzinfo=dateutil.tz.tzutc())
.
import dateutil.tz
cet = dateutil.tz.gettz('CET')
cesttime = datetime.datetime(2010, 4, 1, 12, 57, tzinfo=cet)
cesttime.isoformat()
'2010-04-01T12:57:00+02:00'
cettime = datetime.datetime(2010, 1, 1, 12, 57, tzinfo=cet)
cettime.isoformat()
'2010-01-01T12:57:00+01:00'
# does not automatically parse the time zone portion
dateutil.parser.parse('Feb 25 2010, 16:19:20 CET')\
.replace(tzinfo=dateutil.tz.gettz('CET'))
Unfortunately this technique will be wrong during the repeated daylight savings time hour.