python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.32k stars 440 forks source link

format_datetime no longer applies correct locale information for time period (AM/PM) #956

Closed EdGaere closed 1 year ago

EdGaere commented 1 year ago

Overview Description

The behavior of _formatdatetime has changed starting babel>=2.10.3

In babel==2.9.1, _formatdatetime correctly applies the time period locale. For example, time period "da tarde" is correctly used for locale "pt_PT"

Starting babel>=2.10.3, _formatdatetime no longer applies the time period locale. Time period "p.m." is generated for locale "pt_PT", which is not correct for this locale. Expected "da tarde".

Steps to Reproduce

from datetime import datetime
from babel.dates import format_datetime

format_spec = "a"   # time period (AM/PM) => https://babel.pocoo.org/en/latest/dates.html#time-fields
hours = 13
d = datetime(2007, 1, 1, hours, 0, 0)
locale = "pt_PT"

s = format_datetime(d, locale=locale, format=format_spec)
print(s)

Actual Results

babel==2.9.1 : "da tarde" babel==2.10.3 : "p.m." # < incorrect: locale pt_PT is not used babel==2.11.0 : "p.m." # < incorrect: locale pt_PT is not used

Expected Results

babel==2.9.1 : "da tarde" babel==2.10.3 : "da tarde" babel==2.11.0 : "da tarde"

Reproducibility

Yes. Use code as above.

Additional Information

The correct CLDR information for pt_PT is available in all three versions of Babel tested above.

So the issues appears not related to the locale database, but rather in the rendering of the format in the module.

from babel.localedata import exists, load

locale_name = "pt_PT"
file_exists = exists(locale_name)
assert file_exists

locale_dict = load(locale_name)
print(locale_dict["day_periods"]["format"]["wide"]["pm"])

Output

babel==2.9.1 : "da tarde"
babel==2.10.3 : "da tarde"
babel==2.11.0 : "da tarde"
akx commented 1 year ago

This seems to be correct behavior; with format_spec = "a", you're requesting an abbreviated-width format-context period name for "am" / "pm": https://github.com/unicode-org/cldr/blob/f800890ea86482c2eb5f73224897f4a8bc0b653a/common/main/pt_PT.xml#L1796-L1806

You would need to use "aaaa" (format context, wide width) to get "da tarde".