Added jinja filter pretty_duration_fractional that includes milliseconds
This commit is contained in:
parent
32e25ce129
commit
da14d34551
@ -63,7 +63,7 @@ def remove_private_keys(document):
|
||||
return doc_copy
|
||||
|
||||
|
||||
def pretty_duration(seconds):
|
||||
def pretty_duration(seconds: typing.Union[None, int, float]):
|
||||
if seconds is None:
|
||||
return ''
|
||||
seconds = round(seconds)
|
||||
@ -75,6 +75,27 @@ def pretty_duration(seconds):
|
||||
return f'{minutes:02}:{seconds:02}'
|
||||
|
||||
|
||||
def pretty_duration_fractional(seconds: typing.Union[None, int, float]):
|
||||
if seconds is None:
|
||||
return ''
|
||||
|
||||
# Remove fraction of seconds from the seconds so that the rest is done as integers.
|
||||
seconds, fracs = divmod(seconds, 1)
|
||||
hours, seconds = divmod(int(seconds), 3600)
|
||||
minutes, seconds = divmod(seconds, 60)
|
||||
msec = int(round(fracs * 1000))
|
||||
|
||||
if msec == 0:
|
||||
msec_str = ''
|
||||
else:
|
||||
msec_str = f'.{msec:03}'
|
||||
|
||||
if hours > 0:
|
||||
return f'{hours:02}:{minutes:02}:{seconds:02}{msec_str}'
|
||||
else:
|
||||
return f'{minutes:02}:{seconds:02}{msec_str}'
|
||||
|
||||
|
||||
class PillarJSONEncoder(json.JSONEncoder):
|
||||
"""JSON encoder with support for Pillar resources."""
|
||||
|
||||
|
@ -35,6 +35,10 @@ def format_pretty_duration(s):
|
||||
return pretty_duration(s)
|
||||
|
||||
|
||||
def format_pretty_duration_fractional(s):
|
||||
return pillar.api.utils.pretty_duration_fractional(s)
|
||||
|
||||
|
||||
def format_undertitle(s):
|
||||
"""Underscore-replacing title filter.
|
||||
|
||||
@ -232,6 +236,7 @@ def setup_jinja_env(jinja_env, app_config: dict):
|
||||
jinja_env.filters['pretty_date'] = format_pretty_date
|
||||
jinja_env.filters['pretty_date_time'] = format_pretty_date_time
|
||||
jinja_env.filters['pretty_duration'] = format_pretty_duration
|
||||
jinja_env.filters['pretty_duration_fractional'] = format_pretty_duration_fractional
|
||||
jinja_env.filters['undertitle'] = format_undertitle
|
||||
jinja_env.filters['hide_none'] = do_hide_none
|
||||
jinja_env.filters['pluralize'] = do_pluralize
|
||||
|
@ -41,3 +41,12 @@ class MarkdownTest(unittest.TestCase):
|
||||
'<h1>Title</h1>\n<p>Before</p>\n'
|
||||
'<dl><dt>test</dt><dt>a</dt><dd>b</dd><dt>c</dt><dd>d</dd></dl>\n',
|
||||
jinja.do_markdowned({'eek': '# Title\n\nBefore\n{test a="b" c="d"}'}, 'eek'))
|
||||
|
||||
def test_pretty_duration_fractional(self):
|
||||
from pillar.web import jinja
|
||||
|
||||
self.assertEqual('03:04.568', jinja.format_pretty_duration_fractional(184.5678911111))
|
||||
self.assertEqual('02:03:04.568', jinja.format_pretty_duration_fractional(7384.5678911111))
|
||||
|
||||
self.assertEqual('03:04', jinja.format_pretty_duration_fractional(184.00049))
|
||||
self.assertEqual('02:03:04', jinja.format_pretty_duration_fractional(7384.00049))
|
||||
|
Loading…
x
Reference in New Issue
Block a user