Dateutil is a great tool for parsing dates in string format. for example
from dateutil.parser import parse
parse(\"Tue, 01 Oct 2013 14:26:00 -0300\")
I think the best solution is to subclass the parser from dateutil and use the calendar
lib constants. This is a simple solution, I didn't test it a lot, so use with caution.
It is very simple and will localize dateutil for a lot of languages. Create a module localeparseinfo.py
:
import calendar
from dateutil import parser
class LocaleParserInfo(parser.parserinfo):
WEEKDAYS = zip(calendar.day_abbr, calendar.day_name)
MONTHS = list(zip(calendar.month_abbr, calendar.month_name))[1:]
Now you can use your new parseinfo object as a parameter to dateutil.parser
.
In [1]: import locale;locale.setlocale(locale.LC_ALL, "pt_BR.utf8")
In [2]: from localeparserinfo import LocaleParserInfo
In [3]: from dateutil.parser import parse
In [4]: parse("Ter, 01 Out 2013 14:26:00 -0300", parserinfo=PtParserInfo())
Out[4]: datetime.datetime(2013, 10, 1, 14, 26, tzinfo=tzoffset(None, -10800))
Look that this solves a lot of different language parse, but it is an incomplete solution for all possible dates and times. Take a look at dateutil parser.py
, specially the parserinfo
class variables. Take a look at HMS variable and others.
You can even pass the locale string as an argument to your parserinfo class.