-
-
Notifications
You must be signed in to change notification settings - Fork 31k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate utcnow
and utcfromtimestamp
#103857
Comments
I've gone ahead and done some benchmarks for the most common legitimate use case I've seen for >>> %timeit datetime.now(UTC).replace(tzinfo=None).isoformat(' ')
2.15 µs ± 19.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
>>> %timeit datetime.now(UTC).isoformat(' ')[:-6]
1.61 µs ± 23.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
>>> %timeit datetime.utcnow().isoformat(' ')
919 ns ± 5.23 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) As an example of how this changes the speed in a real-life application, here are the before and after measurements for the change to >>> t = datetime.now().timestamp()
>>> %timeit cookiejar(None) # Uses datetime.now
1.52 µs ± 16.2 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
>>> %timeit cookiejar_utc(None) # Uses datetime.utcnow
1.32 µs ± 6.72 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
>>> %timeit cookiejar(t) # Uses datetime.fromtimestamp
1.77 µs ± 24.2 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
>>> %timeit cookiejar_utc(t) # Uses datetime.utcfromtimestamp
1.4 µs ± 5.75 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) I'm still more or less convinced that this is useful to do, and I'd like to see if anyone complains that it's a major problem after the deprecation before worrying about these micro-optimizations. |
Using `datetime.datetime.utcnow()` and `datetime.datetime.utcfromtimestamp()` will now raise a `DeprecationWarning`. We also have removed our internal uses of these functions and documented the change.
#103858 is now merged, but we still need a What's New entry before closing this, I believe. |
…ning in Python 3.12: python/cpython#103857. Committed via https://github.com/asottile/all-repos
…s New (#104542) Co-authored-by: Paul Ganssle <[email protected]>
I think we're all set here! 🚀 |
utcnow is deprecated and should not be used anymore. this commit swap utcnow() by now(timezone.utc) see: python/cpython#103857 python/cpython#81669 python 3.12 changes related to this: https://docs.python.org/3/whatsnew/3.12.html#deprecated task-3932942
utcnow is deprecated and should not be used anymore. this commit swap utcnow() by now(timezone.utc) see: python/cpython#103857 python/cpython#81669 python 3.12 changes related to this: https://docs.python.org/3/whatsnew/3.12.html#deprecated task-3932942 closes #165799 Signed-off-by: Bertrand Dossogne (bedo) <[email protected]>
They were deprecated in python/cpython#103857. Closes secdev#4460
They were deprecated in python/cpython#103857. Closes secdev#4460
They were deprecated in python/cpython#103857. Closes #4460
…1850) See #11608 and #11497. Starting with [Python 3.12](https://docs.python.org/3/whatsnew/3.12.html), there were changes to datetime: > [datetime](https://docs.python.org/3/library/datetime.html#module-datetime): [datetime.datetime](https://docs.python.org/3/library/datetime.html#datetime.datetime)’s [utcnow()](https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow) and [utcfromtimestamp()](https://docs.python.org/3/library/datetime.html#datetime.datetime.utcfromtimestamp) are deprecated and will be removed in a future version. Instead, use timezone-aware objects to represent datetimes in UTC: respectively, call [now()](https://docs.python.org/3/library/datetime.html#datetime.datetime.now) and [fromtimestamp()](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp) with the tz parameter set to [datetime.UTC](https://docs.python.org/3/library/datetime.html#datetime.UTC). (Contributed by Paul Ganssle in [gh-103857](python/cpython#103857).) The result is that the usage of **utcnow** and **utcfromtimestamp** now throw deprecation warnings when used, ie: > DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). There's a difference of `+00:00` between the old version and the new format. **For utcnow -> now** - `datetime.datetime.utcnow().isoformat()` | `'2025-01-02T19:51:32.579733'` - `datetime.datetime.now(datetime.timezone.utc).isoformat()` | `'2025-01-02T19:51:02.275232+00:00'` **For utcfromtimestamp -> fromtimestamp** Assume that `end_time_ns=1735848645000000000`: - `(datetime.datetime.fromtimestamp(end_time_ns / 1e9, tz=datetime.timezone.utc).replace(microsecond=0).isoformat() + "Z")` - returns `'2025-01-02T20:10:45+00:00Z'` - `(datetime.datetime.utcfromtimestamp(end_time_ns / 1e9).replace(microsecond=0).isoformat() + "Z")` - returns `'2025-01-02T20:10:45Z'` As a result, I attempted remove the trailing ones to be consistent with the old format, but can bring it back. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
Feature or enhancement
Previously, we have documented that
utcnow
andutcfromtimestamp
should not be used, but we didn't go so far as to actually deprecate them, and I wrote a whole article about how you shouldn't use them.The main reason I had for not deprecating them at the time was that
.utcnow()
is faster than.now(datetime.UTC)
, and if you are immediately converting the datetime to a string, likedatetime.utcnow().isoformat()
, there's no danger.I have come around to the idea that this type of use case is not important enough to leave the attractive nuisances of
utcnow()
andutcfromtimestamp()
in place, and we should go ahead and deprecate them.Pitch
We should deprecate them in the documentation and also add
DeprecationWarning
s imploring people not to use them. I'm OK with us saying that we will remove them "at some point in the future" and not necessarily putting a deadline on it, considering how much use ofutcnow()
is out there.Previous discussion
Linked PRs
The text was updated successfully, but these errors were encountered: