python-babel / babel

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

babel.dates.format_date raises exception when using format "B" #1016

Closed fdriesenaar closed 1 year ago

fdriesenaar commented 1 year ago

Overview Description

Hi,

I tried to use format_date with "B" as format and I expected it to work, but instead it raises an exception.

Steps to Reproduce

You can reproduce it by running this script (I used Python 3.8.6 and Babel 2.11.0):

from babel.dates import format_date
from datetime_tz import datetime_tz

start_date = datetime_tz.smartparse("2023-05-01")
print(start_date)
maand = format_date(start_date, format="medium", locale="nl_NL").split(' ')[1]
print(maand)
maand = format_date(start_date, format="B", locale="nl_NL")
print(maand)

Actual Results

This is what I got:

$ python3 t.py
2023-05-01 00:00:00+02:00
mei
Traceback (most recent call last):
  File "t.py", line 8, in <module>
    maand = format_date(start_date, format="B", locale="nl_NL")
  File "/home/fdriesenaar/.local/lib/python3.8/site-packages/babel/dates.py", line 685, in format_date
    return pattern.apply(date, locale)
  File "/home/fdriesenaar/.local/lib/python3.8/site-packages/babel/dates.py", line 1315, in apply
    return self % DateTimeFormat(datetime, locale, reference_date)
  File "/home/fdriesenaar/.local/lib/python3.8/site-packages/babel/dates.py", line 1307, in __mod__
    return self.format % other
  File "/home/fdriesenaar/.local/lib/python3.8/site-packages/babel/dates.py", line 1355, in __getitem__
    return self.format_period(char, num)
  File "/home/fdriesenaar/.local/lib/python3.8/site-packages/babel/dates.py", line 1526, in format_period
    period = get_period_id(self.value, locale=self.locale)
  File "/home/fdriesenaar/.local/lib/python3.8/site-packages/babel/dates.py", line 1128, in get_period_id
    time = _get_time(time, tzinfo)
  File "/home/fdriesenaar/.local/lib/python3.8/site-packages/babel/dates.py", line 208, in _get_time
    if time.tzinfo is None:
AttributeError: 'datetime.date' object has no attribute 'tzinfo'

Expected Results

I expected this outcome:

$ python3 t.py
2023-05-01 00:00:00+02:00
mei
mei

Reproducibility

See above.

Additional Information

None

akx commented 1 year ago

B is "day period" (e.g. a.m, p.m.).

It doesn't work with a date object, since dates doesn't have an associated time. It would work with a time or datetime.

fdriesenaar commented 1 year ago

Hi, thx for your quick and apt response - so, what format is it that I should have used to get the full name of the month?

akx commented 1 year ago

According to the spec it would be MMMM. (These are also in the Babel docs here.)

fdriesenaar commented 1 year ago

Ah, great, thanks again, you can close this issue :)

fdriesenaar commented 1 year ago

A rtfm issue, thx