Sphinx 1.3.x allowed to specify time formatters in today_fmt like %H and %M.

Allow these time formatters again.
This requires to use babel.dates.format_datetime instead of babel.dates.format_date
for formating. The approach of babel compared to ustrftime to use different functions
for time, date and datetime formating creates an ambiguousness for translating %c, %x, %X
from ustrftime to babel. Hence we look out for %x and %X to use the appropriate babel
function in this case.
Change of behaviour: People using the short, medium, long or full babel formats in
today_fmt will now get the respective datetime format instead of just the date format.
This commit is contained in:
Ruediger Pluem
2016-04-05 11:11:59 +02:00
parent 1313726fd6
commit 22dce02bec
+20 -6
View File
@@ -126,15 +126,21 @@ def find_catalog_source_files(locale_dirs, locale, domains=None, gettext_compact
return catalogs
# date_format mappings: ustrftime() to bable.dates.format_date()
# date_format mappings: ustrftime() to bable.dates.format_datetime()
date_format_mappings = {
'%a': 'EEE', # Weekday as locales abbreviated name.
'%A': 'EEEE', # Weekday as locales full name.
'%b': 'MMM', # Month as locales abbreviated name.
'%B': 'MMMM', # Month as locales full name.
'%c': 'medium', # Locales appropriate date and time representation.
'%d': 'dd', # Day of the month as a zero-padded decimal number.
'%H': 'HH', # Hour (24-hour clock) as a decimal number [00,23].
'%I': 'hh', # Hour (12-hour clock) as a decimal number [01,12].
'%j': 'DDD', # Day of the year as a zero-padded decimal number.
'%m': 'MM', # Month as a zero-padded decimal number.
'%M': 'mm', # Minute as a decimal number [00,59].
'%p': 'a', # Locales equivalent of either AM or PM.
'%S': 'ss', # Second as a decimal number.
'%U': 'WW', # Week number of the year (Sunday as the first day of the week)
# as a zero padded decimal number. All days in a new year preceding
# the first Sunday are considered to be in week 0.
@@ -143,21 +149,23 @@ date_format_mappings = {
# as a decimal number. All days in a new year preceding the first
# Monday are considered to be in week 0.
'%x': 'medium', # Locales appropriate date representation.
'%X': 'medium', # Locales appropriate time representation.
'%y': 'YY', # Year without century as a zero-padded decimal number.
'%Y': 'YYYY', # Year with century as a decimal number.
'%Z': 'zzzz', # Time zone name (no characters if no time zone exists).
'%%': '%',
}
def babel_format_date(date, format, locale, warn=None):
def babel_format_date(date, format, locale, warn=None, formatter=babel.dates.format_date):
if locale is None:
locale = 'en'
try:
return babel.dates.format_date(date, format, locale=locale)
return formatter(date, format, locale=locale)
except (ValueError, babel.core.UnknownLocaleError):
# fallback to English
return babel.dates.format_date(date, format, locale='en')
return formatter(date, format, locale='en')
except AttributeError:
if warn:
warn('Invalid date format. Quote the string by single quote '
@@ -184,7 +192,7 @@ def format_date(format, date=None, language=None, warn=None):
warnings.warn('LDML format support will be dropped at Sphinx-1.5',
DeprecationWarning)
return babel_format_date(date, format, locale=language, warn=warn)
return babel_format_date(date, format, locale=language, warn=warn, formatter=babel.dates.format_datetime)
else:
# consider the format as ustrftime's and try to convert it to babel's
result = []
@@ -192,7 +200,13 @@ def format_date(format, date=None, language=None, warn=None):
for token in tokens:
if token in date_format_mappings:
babel_format = date_format_mappings.get(token, '')
result.append(babel_format_date(date, babel_format, locale=language))
if token == '%x':
function = babel.dates.format_date
elif token == '%X':
function = babel.dates.format_time
else:
function = babel.dates.format_datetime
result.append(babel_format_date(date, babel_format, locale=language, formatter=function))
else:
result.append(token)