datetime.utcnow() returns the current date and time in UTC, but as a naive datetime: the numbers are UTC, yet tzinfo is None, so nothing in the object says so.
Because many methods treat naive values as local time, this causes silent bugs. Python 3.12 deprecated it: each call emits a DeprecationWarning. The replacement is datetime.now(timezone.utc), which returns an aware datetime (see now()).
datetime.utcnow() is a classmethod of datetime.datetime: call it on the class itself, as datetime.utcnow(...), and it returns a new object. Import the class first, for example from datetime import datetime. It still works in current versions, but new code should not use it.
One place to change.utc_now() returns an aware UTC datetime and replaces every datetime.utcnow() call.
Convert, then drop the zone.as_naive_utc() first converts to UTC with astimezone(), then removes tzinfo for old code that expects naive values.
Values match. For a UTC input the conversion changes nothing, so the naive copy equals the aware value with its zone stripped.
Fixed check. 16:00 at UTC+05:30 is 10:30 UTC, which the helper returns as a naive 2024-03-15 10:30:00.
Frequently Asked Questions
Yes. It is deprecated since Python 3.12 and emits a DeprecationWarning. Use datetime.now(timezone.utc) instead.
It returns a naive datetime. Methods such as timestamp() and astimezone() assume naive values are local time, so the result is wrong unless the machine runs in UTC.
datetime.now(timezone.utc). If old code needs a naive value, add .replace(tzinfo=None) at the point where you hand it over.
Python hides DeprecationWarning by default unless the call is in __main__ or you run with -W default or a test runner. The warning goes to stderr.
The clock values are the same UTC reading; only the tzinfo differs. utcnow() leaves it as None.