Home  >  Article  >  Backend Development  >  Python: it is now() time to migrate from utcnow()

Python: it is now() time to migrate from utcnow()

PHPz
PHPzOriginal
2024-07-19 16:44:30532browse

Python: it is now() time to migrate from utcnow()

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:

>>> from datetime import datetime
>>> datetime.utcnow()
datetime.datetime(2024, 7, 17, 12, 17, 9, 835551)

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.

(the post continues on my blog https://www.andreagrandi.it/posts/python-now-time-to-migrate-from-utcnow/ )

The above is the detailed content of Python: it is now() time to migrate from utcnow(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn