This morning I randomly found this post from Miguel Grinberg: It’s Time For A Change: datetime.utcnow() Is Now Deprecated.
The main point is that Python utcnow()
method is not timezone aware, and Python 3.12 is deprecating it. Therefore, you should start migrating your code to use now()
instead.
Current state until Python 3.11
Until Python 3.11, the utcnow()
method returns a datetime
object, and you would use it like this:
|
|
The issue with this method is that it doesn’t include timezone information, so you can’t be certain if the time is in UTC or not.
Changes in Python 3.12
In Python 3.12, the utcnow()
method is being deprecated. The new method to use is now()
, with the appropriate parameter to make it timezone aware:
|
|
Start migrating now
The good news is that you can start migrating your code now, even if you are not using Python 3.12 yet. This way, when you upgrade to Python 3.12, you won’t have to worry about this change.