Only use `SHORT_MONTH_DAY_FORMAT` with supported locales

Closes #276
This commit is contained in:
Christopher C. Wells 2021-08-06 08:24:07 -07:00
parent 4c9848ec4e
commit 1aad600f6c
2 changed files with 10 additions and 4 deletions

View File

@ -18,7 +18,8 @@ else:
'%m/%d/%Y %I:%M %p', # '10/25/2006 2:30 PM' '%m/%d/%Y %I:%M %p', # '10/25/2006 2:30 PM'
] ]
# Add custom "short" version of `MONTH_DAY_FORMAT`. # Add custom "short" version of `MONTH_DAY_FORMAT`. This customization will
# only work with the locale format locale specified by this file.
SHORT_MONTH_DAY_FORMAT = 'M j' SHORT_MONTH_DAY_FORMAT = 'M j'
# Append all other input formats from the base locale. # Append all other input formats from the base locale.

View File

@ -31,19 +31,24 @@ def datetime_short(date):
:return: a string representation of `date`. :return: a string representation of `date`.
""" """
now = timezone.now() now = timezone.now()
date_string = None
time_string = None time_string = None
if now.date() == date.date(): if now.date() == date.date():
date_string = _('Today') date_string = _('Today')
time_string = formats.date_format(date, format='TIME_FORMAT') time_string = formats.date_format(date, format='TIME_FORMAT')
elif now.year == date.year: elif now.year == date.year and formats.get_format(
'SHORT_MONTH_DAY_FORMAT') != 'SHORT_MONTH_DAY_FORMAT':
# Use the custom `SHORT_MONTH_DAY_FORMAT` format if available for the
# current locale.
date_string = formats.date_format(date, date_string = formats.date_format(date,
format='SHORT_MONTH_DAY_FORMAT') format='SHORT_MONTH_DAY_FORMAT')
time_string = formats.date_format(date, format='TIME_FORMAT') time_string = formats.date_format(date, format='TIME_FORMAT')
else:
if not date_string:
date_string = formats.date_format(date, format='SHORT_DATETIME_FORMAT') date_string = formats.date_format(date, format='SHORT_DATETIME_FORMAT')
if date_string and time_string: if date_string and time_string:
datetime_string = '{}, {}'.format(date_string, time_string) datetime_string = _('{}, {}').format(date_string, time_string)
else: else:
datetime_string = date_string datetime_string = date_string