Skip to content

Instantly share code, notes, and snippets.

@anpr
Created March 7, 2021 10:34
Show Gist options
  • Save anpr/6535f6f83c8aab8af337226b0043e0c7 to your computer and use it in GitHub Desktop.
Save anpr/6535f6f83c8aab8af337226b0043e0c7 to your computer and use it in GitHub Desktop.
Does fromisoformat product timezone-aware datetimes?
from django.utils import timezone
from datetime import datetime
def is_timezone_aware(dt):
# See https://docs.python.org/3/library/datetime.html#determining-if-an-object-is-aware-or-naive
return dt.tzinfo is not None and dt.tzinfo.utcoffset(d) is not None
now = timezone.now()
is_timezone_aware(now)
# >>> True
now2 = datetime.fromisoformat(now.isoformat())
now == now2
# >>> True
is_timezone_aware(now2)
# >>> True
# Therefore, yes, fromisoformat(d.isoformat()) preserves the "timezone-aware"-ness.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment