Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel cannot parse its own format? #541

Closed
nagylzs opened this issue Nov 11, 2017 · 8 comments
Closed

Babel cannot parse its own format? #541

nagylzs opened this issue Nov 11, 2017 · 8 comments

Comments

@nagylzs
Copy link

nagylzs commented Nov 11, 2017

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
@akx
Copy link
Member

akx commented Nov 11, 2017

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!

@nagylzs
Copy link
Author

nagylzs commented Nov 11, 2017

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.

@nagylzs
Copy link
Author

nagylzs commented Nov 11, 2017

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!

@zero-code-hero
Copy link

zero-code-hero commented Nov 30, 2017

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.

@akx
Copy link
Member

akx commented Jan 15, 2018

@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 :)

@akx
Copy link
Member

akx commented Jan 15, 2018

Since @nagylzs found a solution, closing this issue :)

@akx akx closed this as completed Jan 15, 2018
@cbirger
Copy link

cbirger commented Jun 12, 2018

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!

@mattkiz
Copy link

mattkiz commented Aug 25, 2018

@cbirger Same issue with me running Pycharm on my dev win10 machine, wonder if its on babel's side or my os's

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants