Babel cannot parse its own format?
import babel.dates
import datetime
today = datetime.datetime.today()
formatted = babel.dates.format_date(today, locale='hu')
print(repr(formatted))
'2017. nov. 11.'
babel.dates.parse_date(formatted)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python34-32\lib\site-packages\babel\dates.py", line 1144, in parse_date
format = get_date_format(locale=locale).pattern.lower()
File "C:\Users\User\AppData\Local\Programs\Python\Python34-32\lib\site-packages\babel\dates.py", line 378, in get_date_format
return Locale.parse(locale).date_formats[format]
AttributeError: 'NoneType' object has no attribute 'date_formats'
babel.dates.parse_date(formatted, locale='hu')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python34-32\lib\site-packages\babel\dates.py", line 1165, in parse_date
day = int(numbers[indexes['D']])
IndexError: list index out of range
Hi @nagylzs 👋
I took the liberty of splitting your code into three segments for easier readability :)
It looks like there's a minor problem with Windows there with the first exception; there is no LC_TIME environment variable, so default_locale returns None. The workaround, as you noticed, is to pass in the expected locale yourself.
Anyway, the parse_date function is very simple indeed. It doesn't actually even know how to parse a textual month name, so for that Hungarian format, you're out of luck with parse_date anyway. In the function's defense, though, it's been exactly the same for 11 years now...
It looks like there's a more full-fledged library available for parsing various human-readable date formats, called (aptly enough) dateparser -- it looks like it's capable of parsing Hungarian dates. Hope that helps!
Babel also works fine with format='short'. I have been using python-dateutil before (dateutil.parser.parse) with short formats. I swtiched to babel because it could format dates with month names.
I will look at dateparser. But first: would it be a good contribution to change babel's parse_date method, and add support for parsing (short and long) month names? It does not seem to be a hard problem: month names are already in babel, we just need to look for them.
I just tested dateparser and well, it cannot be beaten. 🥇 I can do things like converting "today" or "one month ago" or even "két héttel ezelőtt" (meaning: "two weeks ago"). All of them are converted to the correct date and time. This is truly amazing!
I just ran into a similar issue when running programs that use babel from monit.
File "/opt/ae_log_browser/lib/python3.5/site-packages/babel/dates.py", line 392, in get_datetime_format
patterns = Locale.parse(locale).datetime_formats
AttributeError: 'NoneType' object has no attribute 'datetime_formats'
Setting LC_TIME or one of the other env vars before babel is imported fixes the issue but it's annoying none the less.
@LPiner This seems to be an unrelated issue, but anyway – I'd expect a "sanely" configured system to have some locale set up if it expects to format things in a locale-dependent way :)
Since @nagylzs found a solution, closing this issue :)
I had this same issue (AttributeError: 'NoneType' object has no attribute 'date_formats') when running my flask application on Google App Engine Standard and calling on the web app to display a table (using the flask_table module) containing a DatetimeCol with a UTC date. Setting the LC_TIME environment variable in the Google App Engine environment (via the app.yaml file) fixed the problem. This thread proved very helpful...thank you!
@cbirger Same issue with me running Pycharm on my dev win10 machine, wonder if its on babel's side or my os's