babel icon indicating copy to clipboard operation
babel copied to clipboard

babel.dates.parse_date() doesn't allow you to specify a format/pattern

Open johanndt opened this issue 9 years ago • 6 comments

We've started using Babel recently but ran into an issue where babel.dates.parse_date() doesn't allow us to specify the format to use. It always uses the 'medium' format. And for some formats that fails. Eg. when using 'en_ZA', you can't parse a short format date.

Internally the first thing it does is call get_date_format() without being explicit about the format, thus the default 'medium' format always gets used.

I'm happy to submit a pull-request but need some guidance on what type of fix would be acceptable. To stick with the signature order used everywhere else would require a change to parse_date(string, format='medium', locale=LC_TIME) but this would break people's code who were using only 2 positional arguments.

Can we add it at the end eg. parse_date(string, locale=LC_TIME, format='medium')?

Or should we create a new function eg: parse_formatted_date(string, format='medium', locale=LC_TIME)? The existing function name can then just be a wrapper around the new function.

Hope all this made sense. Thanks!

johanndt avatar Jul 14 '16 22:07 johanndt

+1

We ran into this too and hacked around it and I never got around to logging an issue for it. ^_^

I like the approach of adding a kwarg to the existing parse_date fn. It nicely mirrors format_date.

jtwang avatar Jul 14 '16 22:07 jtwang

@jtwang you should see our hackish workaround! Monkey patching get_date_format in the library with a partial to always return the format we want. :)

johanndt avatar Jul 15 '16 07:07 johanndt

Since we're aiming for 3.0 next (at least that's in my plans :D) and API compatibility is not guaranteed, I think for symmetry/consistency locale should remain the last parameter, and format added before it.

akx avatar Jul 15 '16 08:07 akx

Also, I believe people mostly use the locale=... kwarg form to pass the locale in to Babel anyway :)

akx avatar Jul 15 '16 08:07 akx

Thanks @akx. So if we aim for 3.0 should the PR just be against master?

johanndt avatar Jul 15 '16 09:07 johanndt

Sure, @johanndt :) And thanks in advance!

akx avatar Jul 15 '16 09:07 akx