mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #1446 from avaris/enhanced_strftime
Fixes #1395: extends pelican.utils.strftime with `-` prefix to strip leading zeros
This commit is contained in:
commit
1fae9534d5
3 changed files with 33 additions and 5 deletions
|
|
@ -345,8 +345,12 @@ Date format and locale
|
||||||
If no ``DATE_FORMATS`` are set, Pelican will fall back to
|
If no ``DATE_FORMATS`` are set, Pelican will fall back to
|
||||||
``DEFAULT_DATE_FORMAT``. If you need to maintain multiple languages with
|
``DEFAULT_DATE_FORMAT``. If you need to maintain multiple languages with
|
||||||
different date formats, you can set the ``DATE_FORMATS`` dictionary using the
|
different date formats, you can set the ``DATE_FORMATS`` dictionary using the
|
||||||
language name (``lang`` metadata in your post content) as the key. Regarding
|
language name (``lang`` metadata in your post content) as the key.
|
||||||
available format codes, see `strftime document of python`_ :
|
|
||||||
|
In addition to the standard C89 strftime format codes that are listed in
|
||||||
|
`Python strftime documentation`_, you can use ``-`` character between ``%`` and
|
||||||
|
the format character to remove any leading zeros. For example, ``%d/%m/%Y`` will
|
||||||
|
output ``01/01/2014`` whereas ``%-d/%-m/%Y`` will result in ``1/1/2014``.
|
||||||
|
|
||||||
.. parsed-literal::
|
.. parsed-literal::
|
||||||
|
|
||||||
|
|
@ -385,7 +389,7 @@ can get a list of available locales via the ``locale -a`` command; see manpage
|
||||||
`locale(1)`_ for more information.
|
`locale(1)`_ for more information.
|
||||||
|
|
||||||
|
|
||||||
.. _strftime document of python: http://docs.python.org/library/datetime.html#strftime-strptime-behavior
|
.. _Python strftime documentation: http://docs.python.org/library/datetime.html#strftime-strptime-behavior
|
||||||
|
|
||||||
.. _locales on Windows: http://msdn.microsoft.com/en-us/library/cdax410z%28VS.71%29.aspx
|
.. _locales on Windows: http://msdn.microsoft.com/en-us/library/cdax410z%28VS.71%29.aspx
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -283,6 +283,13 @@ class TestUtils(LoggedTestCase):
|
||||||
self.assertEqual(utils.strftime(d, '%d/%m/%Y Øl trinken beim Besäufnis'),
|
self.assertEqual(utils.strftime(d, '%d/%m/%Y Øl trinken beim Besäufnis'),
|
||||||
'29/08/2012 Øl trinken beim Besäufnis')
|
'29/08/2012 Øl trinken beim Besäufnis')
|
||||||
|
|
||||||
|
# alternative formatting options
|
||||||
|
self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '29/8/12')
|
||||||
|
self.assertEqual(utils.strftime(d, '%-H:%-M:%-S'), '0:0:0')
|
||||||
|
|
||||||
|
d = utils.SafeDatetime(2012, 8, 9)
|
||||||
|
self.assertEqual(utils.strftime(d, '%-d/%-m/%y'), '9/8/12')
|
||||||
|
|
||||||
|
|
||||||
# test the output of utils.strftime in a different locale
|
# test the output of utils.strftime in a different locale
|
||||||
# Turkish locale
|
# Turkish locale
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,11 @@ def strftime(date, date_format):
|
||||||
replacing formatted output back.
|
replacing formatted output back.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
c89_directives = 'aAbBcdfHIjmMpSUwWxXyYzZ%'
|
||||||
|
strip_zeros = lambda x: x.lstrip('0') or '0'
|
||||||
|
|
||||||
# grab candidate format options
|
# grab candidate format options
|
||||||
format_options = '%.'
|
format_options = '%[-]?.'
|
||||||
candidates = re.findall(format_options, date_format)
|
candidates = re.findall(format_options, date_format)
|
||||||
|
|
||||||
# replace candidates with placeholders for later % formatting
|
# replace candidates with placeholders for later % formatting
|
||||||
|
|
@ -56,14 +59,28 @@ def strftime(date, date_format):
|
||||||
formatted_candidates = []
|
formatted_candidates = []
|
||||||
for candidate in candidates:
|
for candidate in candidates:
|
||||||
# test for valid C89 directives only
|
# test for valid C89 directives only
|
||||||
if candidate[1] in 'aAbBcdfHIjmMpSUwWxXyYzZ%':
|
if candidate[-1] in c89_directives:
|
||||||
|
# check for '-' prefix
|
||||||
|
if len(candidate) == 3:
|
||||||
|
# '-' prefix
|
||||||
|
candidate = '%{}'.format(candidate[-1])
|
||||||
|
conversion = strip_zeros
|
||||||
|
else:
|
||||||
|
conversion = None
|
||||||
|
|
||||||
|
# format date
|
||||||
if isinstance(date, SafeDatetime):
|
if isinstance(date, SafeDatetime):
|
||||||
formatted = date.strftime(candidate, safe=False)
|
formatted = date.strftime(candidate, safe=False)
|
||||||
else:
|
else:
|
||||||
formatted = date.strftime(candidate)
|
formatted = date.strftime(candidate)
|
||||||
|
|
||||||
# convert Py2 result to unicode
|
# convert Py2 result to unicode
|
||||||
if not six.PY3 and enc is not None:
|
if not six.PY3 and enc is not None:
|
||||||
formatted = formatted.decode(enc)
|
formatted = formatted.decode(enc)
|
||||||
|
|
||||||
|
# strip zeros if '-' prefix is used
|
||||||
|
if conversion:
|
||||||
|
formatted = conversion(formatted)
|
||||||
else:
|
else:
|
||||||
formatted = candidate
|
formatted = candidate
|
||||||
formatted_candidates.append(formatted)
|
formatted_candidates.append(formatted)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue