I'm using strptime
like this:
import time
time.strptime("+10:00","+%H:%M")
but "+10:00" could also be "-10:00" (timezone offset from UTC) which would break the above command. I could use
time.strptime("+10:00"[1:],"%H:%M")
but ideally I'd find it more readable to use a wildcard in front of the format code.
Does such a wildcard operator exist for Python's strptime
/ strftime
?
There is no wildcard operator. The list of format directives supported by
strptime
is in the docs.What you're looking for is the
%z
format directive, which supports a representation of the timezone of the form+HHMM
or-HHMM
. While it has been supported bydatetime.strftime
for some time, it is only supported instrptime
starting in Python 3.2.On Python 2, the best way to handle this is probably to use
datetime.datetime.strptime
, manually handle the negative offset, and get adatetime.timedelta
:In Python 3.2, remove the
:
and use%z
:We developed datetime-glob to parse date/times from a list of files generated by a consistent date/time formatting. From the module's documentation: