From 71758a46c64e31f60f3da741146c14d4880bdacf Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 25 Feb 2026 15:14:56 -0600 Subject: [PATCH] fix: convert datetime values to local zone for `app.render_date()` --- src/wuttjamaican/app.py | 15 ++++++++++++--- tests/test_app.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/wuttjamaican/app.py b/src/wuttjamaican/app.py index 6314f9d..3f7dbe6 100644 --- a/src/wuttjamaican/app.py +++ b/src/wuttjamaican/app.py @@ -930,19 +930,28 @@ class AppHandler: # pylint: disable=too-many-public-methods :meth:`render_datetime()`. """ - def render_date(self, value): + def render_date(self, value, local=True): """ Return a human-friendly display string for the given date. Uses :attr:`display_format_date` to render the value. - :param value: A :class:`python:datetime.date` instance (or - ``None``). + :param value: Can be a :class:`python:datetime.date` *or* + :class:`python:datetime.datetime` instance, or ``None``. + + :param local: By default the ``value`` will first be passed to + :meth:`localtime()` to normalize it for display (but only + if value is a ``datetime`` instance). Specify + ``local=False`` to skip that and render the value as-is. :returns: Display string. """ if value is None: return "" + + if local and isinstance(value, datetime.datetime): + value = self.localtime(value) + return value.strftime(self.display_format_date) def render_datetime(self, value, local=True, html=False): diff --git a/tests/test_app.py b/tests/test_app.py index 78a8862..bca63bc 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -619,11 +619,28 @@ app_title = WuttaTest self.assertEqual(self.app.render_currency(value), "($42.42)") def test_render_date(self): + self.config.setdefault("wuttatest.timezone.default", "America/Los_Angeles") + tzlocal = get_timezone_by_name("America/Los_Angeles") + + # null self.assertEqual(self.app.render_date(None), "") + # basic date dt = datetime.date(2024, 12, 11) self.assertEqual(self.app.render_date(dt), "2024-12-11") + # local/aware datetime + dt = datetime.datetime(2024, 12, 11, 20, 30, tzinfo=tzlocal) + self.assertEqual(self.app.render_date(dt), "2024-12-11") + + # naive UTC, but convert to local + dt = datetime.datetime(2024, 12, 12, 2, 30) + self.assertEqual(self.app.render_date(dt), "2024-12-11") + + # naive UTC, no local conversion + dt = datetime.datetime(2024, 12, 12, 2, 30) + self.assertEqual(self.app.render_date(dt, local=False), "2024-12-12") + def test_render_datetime(self): self.config.setdefault("wuttatest.timezone.default", "America/Los_Angeles") tzlocal = get_timezone_by_name("America/Los_Angeles")