datetime.py 751 B

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