Created
March 7, 2021 10:34
-
-
Save anpr/6535f6f83c8aab8af337226b0043e0c7 to your computer and use it in GitHub Desktop.
Does fromisoformat product timezone-aware datetimes?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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