Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are so many footguns in the datetime lib.

That's why I use a Flake8 plugin to prohibit especially egregious footguns.

https://github.com/jkittner/flake8-ban-utcnow



Honestly yeah who in tarnation created that function and called it utcnow

These things are really frustrating


Yeah, utcnow is completely broken. They should have fixed it when they created datetime.timezone.utc, but they didn't. The recommendation is to use datetime.datetime.now(datetime.timezone.utc) instead. utcnow should be deprecated and eventually removed.


I recently learned that there is datetime.UTC which is an alias of datetime.timezone.utc

It helps a bit with the verbosity:

from datetime import datetime, UTC

datetime.now(UTC)


That's from 3.11 onwards.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: