Concept question. Why does datetime.utcnow()
return a naive date:
from datetime import datetime
datetime.utcnow()
instead of the time with UTC timezone specified like this:
from datetime import datetime, timezone
datetime.utcnow().replace(tzinfo=timezone.utc)
The datetime.py source indicates this is on purpose.
@classmethod
def utcnow(cls):
"Construct a UTC datetime from time.time()."
t = _time.time()
return cls.utcfromtimestamp(t)
@classmethod
def utcfromtimestamp(cls, t):
"""Construct a naive UTC datetime from a POSIX timestamp."""
return cls._fromtimestamp(t, True, None)
Trying to learn the thinking behind it. Thank you.
Edit: From the linked question here (thank you) this appears to be the preferred approach in Python 3.2+:
from datetime import datetime, timezone
datetime.datetime.now(datetime.timezone.utc)