Procházet zdrojové kódy

Fixes #11384: Correct current time display on script & report forms

jeremystretch před 3 roky
rodič
revize
0669fda1fd

+ 1 - 0
docs/release-notes/version-3.4.md

@@ -11,6 +11,7 @@
 
 ### Bug Fixes
 
+* [#11384](https://github.com/netbox-community/netbox/issues/11384) - Correct current time display on script & report forms
 * [#11403](https://github.com/netbox-community/netbox/issues/11403) - Fix exception when scheduling a job in the past
 
 ---

+ 2 - 1
netbox/extras/forms/reports.py

@@ -3,6 +3,7 @@ from django.utils import timezone
 from django.utils.translation import gettext as _
 
 from utilities.forms import BootstrapMixin, DateTimePicker, SelectDurationWidget
+from utilities.utils import local_now
 
 __all__ = (
     'ReportForm',
@@ -35,5 +36,5 @@ class ReportForm(BootstrapMixin, forms.Form):
         super().__init__(*args, **kwargs)
 
         # Annotate the current system time for reference
-        now = timezone.now().strftime('%Y-%m-%d %H:%M:%S')
+        now = local_now().strftime('%Y-%m-%d %H:%M:%S')
         self.fields['schedule_at'].help_text += f' (current time: <strong>{now}</strong>)'

+ 2 - 2
netbox/extras/forms/scripts.py

@@ -1,8 +1,8 @@
 from django import forms
-from django.utils import timezone
 from django.utils.translation import gettext as _
 
 from utilities.forms import BootstrapMixin, DateTimePicker, SelectDurationWidget
+from utilities.utils import local_now
 
 __all__ = (
     'ScriptForm',
@@ -34,7 +34,7 @@ class ScriptForm(BootstrapMixin, forms.Form):
         super().__init__(*args, **kwargs)
 
         # Annotate the current system time for reference
-        now = timezone.now().strftime('%Y-%m-%d %H:%M:%S')
+        now = local_now().strftime('%Y-%m-%d %H:%M:%S')
         self.fields['_schedule_at'].help_text += f' (current time: <strong>{now}</strong>)'
 
         # Move _commit and _schedule_at to the end of the form

+ 9 - 0
netbox/utilities/utils.py

@@ -12,6 +12,8 @@ from django.db.models import Count, OuterRef, Subquery
 from django.db.models.functions import Coalesce
 from django.http import QueryDict
 from django.utils.html import escape
+from django.utils import timezone
+from django.utils.timezone import localtime
 from jinja2.sandbox import SandboxedEnvironment
 from mptt.models import MPTTModel
 
@@ -527,3 +529,10 @@ def highlight_string(value, highlight, trim_pre=None, trim_post=None, trim_place
         post = post[:trim_post] + trim_placeholder
 
     return f'{escape(pre)}<mark>{escape(match)}</mark>{escape(post)}'
+
+
+def local_now():
+    """
+    Return the current date & time in the system timezone.
+    """
+    return localtime(timezone.now())