Python timezone '%z' directive for datetime.strptime() not available -
using '%z' pattern of datetime.strptime()
i have string text represent date , i'm able parse , transform clean datetime object:
date = "[24/aug/2014:17:57:26" dt = datetime.strptime(date, "[%d/%b/%y:%h:%m:%s")
except can't catch entire date string the timezone using %z pattern specified here
date_tz = 24/aug/2014:17:57:26 +0200 dt = datetime.strptime(date, "[%d/%b/%y:%h:%m:%s %z]") >>> valueerror: 'z' bad directive in format '[%d/%b/%y:%h:%m:%s %z]'
because this bug report says
strftime() implemented per platform
i precise there no such problem naive tzinfo directive '%z'
workaround : casting tzinfo string tzinfo object
i can make following workaround transforming gst time format string tzinfo object [as suggested here][4] using dateutil module , insert tzinfo datetime object
question: make %z available plateform?
but need %z pattern further project find solution avoid workaround , using external module simple task. can suggest me reading on it? supposed newer version of python (i'm on 2.7) can handle i'd rather not changing version little crucial detail.
[edit]
well, seeing comments make me reformulated question how parse email time zone indicator using strptime() without being aware of locale time?
strptime()
implemented in pure python. unlike strftime()
; [which directives supported] doesn't depend on platform. %z
supported since python 3.2:
>>> datetime import datetime >>> datetime.strptime('24/aug/2014:17:57:26 +0200', '%d/%b/%y:%h:%m:%s %z') datetime.datetime(2014, 8, 24, 17, 57, 26, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))
how parse email time zone indicator using strptime() without being aware of locale time?
there no concrete timezone implementation in python 2.7. implement utc offset parsing, see how parse dates -0400 timezone string in python?
Comments
Post a Comment