Merge pull request #4510 from tk0miya/4275_update_strftime

Fix #4275: Formats accepted by sphinx.util.i18n.format_date are limite
This commit is contained in:
Takeshi KOMIYA 2018-01-28 21:04:23 +09:00 committed by GitHub
commit 4b89d5900a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 27 deletions

View File

@ -28,6 +28,7 @@ Bugs fixed
* #4490: autodoc: type annotation is broken with python 3.7.0a4+
* utils package is installed
* #3952: apidoc: module header is too escaped
* #4275: Formats accepted by sphinx.util.i18n.format_date are limited
Testing
--------

View File

@ -157,21 +157,30 @@ date_format_mappings = {
'%b': 'MMM', # Month as locales abbreviated name.
'%B': 'MMMM', # Month as locales full name.
'%c': 'medium', # Locales appropriate date and time representation.
'%-d': 'd', # Day of the month as a decimal number.
'%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].
'%-H': 'H', # Hour (24-hour clock) as a decimal number [0,23].
'%H': 'HH', # Hour (24-hour clock) as a zero-padded decimal number [00,23].
'%-I': 'h', # Hour (12-hour clock) as a decimal number [1,12].
'%I': 'hh', # Hour (12-hour clock) as a zero-padded decimal number [01,12].
'%-j': 'D', # Day of the year as a decimal number.
'%j': 'DDD', # Day of the year as a zero-padded decimal number.
'%-m': 'M', # Month as a decimal number.
'%m': 'MM', # Month as a zero-padded decimal number.
'%M': 'mm', # Minute as a decimal number [00,59].
'%-M': 'm', # Minute as a decimal number [0,59].
'%M': 'mm', # Minute as a zero-padded decimal number [00,59].
'%p': 'a', # Locales equivalent of either AM or PM.
'%S': 'ss', # Second as a decimal number.
'%-S': 's', # Second as a decimal number.
'%S': 'ss', # Second as a zero-padded 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.
'%w': 'e', # Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
'%W': 'WW', # Week number of the year (Monday as the first day of the week)
'%-W': 'W', # Week number of the year (Monday as the first day of the week)
# as a decimal number. All days in a new year preceding the first
# Monday are considered to be in week 0.
'%W': 'WW', # Week number of the year (Monday as the first day of the week)
# as a zero-padded decimal number.
'%x': 'medium', # Locales appropriate date representation.
'%X': 'medium', # Locales appropriate time representation.
'%y': 'YY', # Year without century as a zero-padded decimal number.
@ -180,6 +189,8 @@ date_format_mappings = {
'%%': '%',
}
date_format_re = re.compile('(%s)' % '|'.join(date_format_mappings))
def babel_format_date(date, format, locale, formatter=babel.dates.format_date):
# type: (datetime, unicode, unicode, Callable) -> unicode
@ -214,7 +225,7 @@ def format_date(format, date=None, language=None):
date = datetime.now()
result = []
tokens = re.split('(%.)', format)
tokens = date_format_re.split(format)
for token in tokens:
if token in date_format_mappings:
babel_format = date_format_mappings.get(token, '')

View File

@ -176,6 +176,8 @@ def test_format_date():
datet = datetime.datetime(2016, 2, 7, 5, 11, 17, 0)
assert i18n.format_date(format, date=datet) == 'February 07, 2016, 05:11:17 05 AM'
format = '%B %-d, %Y, %-H:%-M:%-S %-I %p'
assert i18n.format_date(format, date=datet) == 'February 7, 2016, 5:11:17 5 AM'
format = '%x'
assert i18n.format_date(format, date=datet) == 'Feb 7, 2016'
format = '%X'