Open vashek opened 1 year ago
I am -1 on this:
localnow_naive()
as an alias tonow()
I'm not sure people will discover this or that it would be obvious what it does and why. It particularly doesn't make sense without the other three.
localnow_aware()
as an alias tonow().astimezone()
This is only pseudo-aware and I don't think we should be encouraging people to do this by default. I think they'll think that they are getting a proper aware time zone (like one with a ZoneInfo
object), and not realize that they are effectively doing the same thing as pytz.timezone(my_timezone).localize(datetime.now())
, which won't work properly with arithmetic. The proper way to work with local times is to keep them naïve until you need to access utcoffset
or tzname
.
utcnow_naive()
as an alias toutcnow()
We should deprecate utcnow
.
utcnow_aware()
as an alias tonow(timezone.utc)
This is the one I'm most sympathetic to, but also I think that it's only useful when people are expecting to find a utcnow
method, and as I mentioned above I think we can deprecate it. I think datetime.now(UTC)
is a perfectly reasonable pattern.
Feature or enhancement
I propose adding the following class methods to
datetime.datetime
:localnow_naive()
as an alias tonow()
localnow_aware()
as an alias tonow().astimezone()
utcnow_naive()
as an alias toutcnow()
utcnow_aware()
as an alias tonow(timezone.utc)
Pitch
It is currently not very obvious that
datetime.now()
anddatetime.utcnow()
return naivedatetime
objects. In fact, arguablydatetime.utcnow()
is misleading as one might expect it to return time with the UTC timezone.It is also not very obvious how to obtain the aware versions. (At least, it's not to me; I find myself looking it up repeatedly.)
These aliases make it very obvious which version is returned and would be easily discoverable.
Previous discussion
A thread is open at https://discuss.python.org/t/add-aliases-with-explicit-names-for-naive-and-aware-datetime-now-and-utcnow/25997
A related idea was discussed at https://discuss.python.org/t/get-now-with-local-system-timezone-without-knowing-the-timezone-first/22665
Linked PRs