From befdb3883806a48f9d61af3c91b150432d4c8731 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 1 Jul 2022 16:53:51 +0200 Subject: use locale-aware datetime formatting --- development.ini | 1 + fietsboek/jinja2.py | 19 +++++++++++++++++++ fietsboek/templates/details.jinja2 | 8 ++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/development.ini b/development.ini index fb46605..153e684 100644 --- a/development.ini +++ b/development.ini @@ -18,6 +18,7 @@ pyramid.includes = pyramid_debugtoolbar jinja2.filters = format_decimal = fietsboek.jinja2.filter_format_decimal + format_datetime = fietsboek.jinja2.filter_format_datetime sqlalchemy.url = sqlite:///%(here)s/fietsboek.sqlite diff --git a/fietsboek/jinja2.py b/fietsboek/jinja2.py index b940d74..def61a6 100644 --- a/fietsboek/jinja2.py +++ b/fietsboek/jinja2.py @@ -2,6 +2,7 @@ import jinja2 from babel.numbers import format_decimal +from babel.dates import format_datetime @jinja2.pass_context @@ -14,7 +15,25 @@ def filter_format_decimal(ctx, value): :type ctx: jinja2.runtime.Context :param value: The value to format. :type value: float + :return: The formatted decimal. + :rtype: str """ request = ctx.get('request') locale = request.localizer.locale_name return format_decimal(value, locale=locale) + + +@jinja2.pass_context +def filter_format_datetime(ctx, value): + """Format a datetime according to the locale. + + :param ctx: The jinja context, passed automatically. + :type ctx: jinja2.runtime.Context + :param value: The value to format. + :type value: datetime.datetime + :return: The formatted date. + :rtype: str + """ + request = ctx.get('request') + locale = request.localizer.locale_name + return format_datetime(value, locale=locale) diff --git a/fietsboek/templates/details.jinja2 b/fietsboek/templates/details.jinja2 index bf87b9f..f626730 100644 --- a/fietsboek/templates/details.jinja2 +++ b/fietsboek/templates/details.jinja2 @@ -61,15 +61,15 @@ {{ _("page.details.date") }} - {{ track.date }} + {{ track.date | format_datetime }} {{ _("page.details.start_time") }} - {{ track.start_time }} + {{ track.start_time | format_datetime }} {{ _("page.details.end_time") }} - {{ track.end_time }} + {{ track.end_time | format_datetime }} {{ _("page.details.length") }} @@ -125,7 +125,7 @@ {{ comment_md_to_html(comment.text) }} {% endfor %} -- cgit v1.2.3