I was going through the release notes of the new Python 3.12 version the other day, and one item caught my attention in the deprecations section: If y

It's Time For A Change: datetime.utcnow() Is Now Deprecated

submited by
Style Pass
2023-11-18 14:00:12

I was going through the release notes of the new Python 3.12 version the other day, and one item caught my attention in the deprecations section:

If you have followed my web development tutorials you must have seen me use utcnow() a lot, so I will clearly need to re-train myself to use an alternative, in preparation for the eventual removal of this function (likely a few years out, so no need to panic!).

The problem that the Python maintainers have found comes from the fact that these functions return "naive" datetime objects. A naive datetime object is one that does not have a timezone, which means that it can only be used in a context where the timezone does not matter or is already known in advance. This is in contrast to "aware" datetime objects, which do have a timezone attached to them explicitly.

If you ask me, I think the names of these functions are misleading. A function that is called utcnow() should be expected to return UTC datetimes, as implied by the name. I would have made it more clear that these functions work with naive time, maybe by calling them naive_utcnow() and naive_utcfromtimestamp().

Leave a Comment