From 1d2ea90fd4c79ad9149b6b3826a37f31853d227a Mon Sep 17 00:00:00 2001 From: bctiemann Date: Mon, 26 Aug 2024 14:37:20 -0400 Subject: [PATCH] Fixes: #17108 - Update isotime and isodate filters to be timezone-aware (#17267) * Update isotime and isodate filters to be timezone-aware for display in templates (particularly Scripts) * Handle naive datetimes gracefully --- netbox/extras/forms/reports.py | 2 +- netbox/extras/forms/scripts.py | 2 +- netbox/utilities/templatetags/builtins/filters.py | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/netbox/extras/forms/reports.py b/netbox/extras/forms/reports.py index 358ee90e345..95692b3f625 100644 --- a/netbox/extras/forms/reports.py +++ b/netbox/extras/forms/reports.py @@ -31,7 +31,7 @@ def __init__(self, *args, scheduling_enabled=True, **kwargs): super().__init__(*args, **kwargs) # Annotate the current system time for reference - now = local_now().strftime('%Y-%m-%d %H:%M:%S') + now = local_now().strftime('%Y-%m-%d %H:%M:%S %Z') self.fields['schedule_at'].help_text += _(' (current time: {now})').format(now=now) # Remove scheduling fields if scheduling is disabled diff --git a/netbox/extras/forms/scripts.py b/netbox/extras/forms/scripts.py index ece96f5e492..331f7f01f96 100644 --- a/netbox/extras/forms/scripts.py +++ b/netbox/extras/forms/scripts.py @@ -37,7 +37,7 @@ def __init__(self, *args, scheduling_enabled=True, **kwargs): super().__init__(*args, **kwargs) # Annotate the current system time for reference - now = local_now().strftime('%Y-%m-%d %H:%M:%S') + now = local_now().strftime('%Y-%m-%d %H:%M:%S %Z') self.fields['_schedule_at'].help_text += _(' (current time: {now})').format(now=now) # Remove scheduling fields if scheduling is disabled diff --git a/netbox/utilities/templatetags/builtins/filters.py b/netbox/utilities/templatetags/builtins/filters.py index 738b9a23e37..b1c8c524bee 100644 --- a/netbox/utilities/templatetags/builtins/filters.py +++ b/netbox/utilities/templatetags/builtins/filters.py @@ -8,6 +8,7 @@ from django.contrib.humanize.templatetags.humanize import naturalday, naturaltime from django.utils.html import escape from django.utils.safestring import mark_safe +from django.utils.timezone import localtime from markdown import markdown from markdown.extensions.tables import TableExtension @@ -218,7 +219,8 @@ def isodate(value): text = value.isoformat() return mark_safe(f'{text}') elif type(value) is datetime.datetime: - text = value.date().isoformat() + local_value = localtime(value) if value.tzinfo else value + text = local_value.date().isoformat() return mark_safe(f'{text}') else: return '' @@ -229,7 +231,8 @@ def isotime(value, spec='seconds'): if type(value) is datetime.time: return value.isoformat(timespec=spec) if type(value) is datetime.datetime: - return value.time().isoformat(timespec=spec) + local_value = localtime(value) if value.tzinfo else value + return local_value.time().isoformat(timespec=spec) return ''