Introduce datetime_from_timestamp() utility function

This commit is contained in:
Jeremy Stretch 2024-07-16 12:19:32 -04:00
parent cf1024a12b
commit b2c5a4639c
3 changed files with 20 additions and 6 deletions

View File

@ -21,6 +21,7 @@ from netbox.models import ChangeLoggedModel
from netbox.models.features import CloningMixin, ExportTemplatesMixin from netbox.models.features import CloningMixin, ExportTemplatesMixin
from netbox.search import FieldTypes from netbox.search import FieldTypes
from utilities import filters from utilities import filters
from utilities.datetime import datetime_from_timestamp
from utilities.forms.fields import ( from utilities.forms.fields import (
CSVChoiceField, CSVModelChoiceField, CSVModelMultipleChoiceField, CSVMultipleChoiceField, DynamicChoiceField, CSVChoiceField, CSVModelChoiceField, CSVModelMultipleChoiceField, CSVMultipleChoiceField, DynamicChoiceField,
DynamicModelChoiceField, DynamicModelMultipleChoiceField, DynamicMultipleChoiceField, JSONField, LaxURLField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, DynamicMultipleChoiceField, JSONField, LaxURLField,
@ -672,12 +673,8 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
# Validate date & time # Validate date & time
elif self.type == CustomFieldTypeChoices.TYPE_DATETIME: elif self.type == CustomFieldTypeChoices.TYPE_DATETIME:
if type(value) is not datetime: if type(value) is not datetime:
# Work around UTC issue for Python < 3.11; see
# https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat
if type(value) is str and value.endswith('Z'):
value = f'{value[:-1]}+00:00'
try: try:
datetime.fromisoformat(value) datetime_from_timestamp(value)
except ValueError: except ValueError:
raise ValidationError( raise ValidationError(
_("Date and time values must be in ISO 8601 format (YYYY-MM-DD HH:MM:SS).") _("Date and time values must be in ISO 8601 format (YYYY-MM-DD HH:MM:SS).")

View File

@ -1,7 +1,10 @@
import datetime
from django.utils import timezone from django.utils import timezone
from django.utils.timezone import localtime from django.utils.timezone import localtime
__all__ = ( __all__ = (
'datetime_from_timestamp',
'local_now', 'local_now',
) )
@ -11,3 +14,15 @@ def local_now():
Return the current date & time in the system timezone. Return the current date & time in the system timezone.
""" """
return localtime(timezone.now()) return localtime(timezone.now())
def datetime_from_timestamp(value):
"""
Convert an ISO 8601 or RFC 3339 timestamp to a datetime object.
"""
# Work around UTC issue for Python < 3.11; see
# https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat
# TODO: Remove this once Python 3.10 is no longer supported
if type(value) is str and value.endswith('Z'):
value = f'{value[:-1]}+00:00'
return datetime.datetime.fromisoformat(value)

View File

@ -6,6 +6,8 @@ from typing import Union
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from utilities.datetime import datetime_from_timestamp
RELEASE_PATH = 'release.yaml' RELEASE_PATH = 'release.yaml'
LOCAL_RELEASE_PATH = 'local/release.yaml' LOCAL_RELEASE_PATH = 'local/release.yaml'
@ -52,6 +54,6 @@ def load_release_data():
# Convert the published date to a date object # Convert the published date to a date object
if 'published' in data: if 'published' in data:
data['published'] = datetime.date.fromisoformat(data['published']) data['published'] = datetime_from_timestamp(data['published'])
return ReleaseInfo(**data) return ReleaseInfo(**data)