GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Deprecate `utcnow` and `utcfromtimestamp` · Issue #103857 · python/cpython

Deprecate utcnow and utcfromtimestamp #103857

New issue
Copy link
New issue
Copy link
Closed
Closed
Deprecate utcnow and utcfromtimestamp#103857
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

@pganssle
pganssle
opened on Apr 25, 2023
Issue body actions

Feature or enhancement

Previously, we have documented that utcnow and utcfromtimestamp 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, like datetime.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() and utcfromtimestamp() in place, and we should go ahead and deprecate them.

Pitch

We should deprecate them in the documentation and also add DeprecationWarnings 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 of utcnow() is out there.

Previous discussion

  • Document the "gotcha" behaviors in utcnow() and utcfromtimestamp() #81669
  • https://discuss.python.org/t/add-aliases-with-explicit-names-for-naive-and-aware-datetime-now-and-utcnow/25997
  • Various bugs about this: datetime.utcnow() should return a timezone aware datetime #90477, datetime.datetime.utcnow should return a UTC timestamp #56965, Using datetime.datetime.utcnow().timestamp() in Python3.6.0 can't get correct UTC timestamp. #77474

Linked PRs

  • GH-103857: Deprecate utcnow and utcfromtimestamp #103858
  • gh-103857: Update deprecation stacktrace to point to calling line #104431
  • gh-103857: Document utcnow and utcfromtimestamp deprecations in What's New #104542
Reacted by Łukasz Langa, David Lord, Shantanu and Razzi Abuissa
Reacted by Glenn Maynard

Activity

Next
  1. pganssle
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Apr 25, 2023
  2. pganssle commented on Apr 26, 2023

    @pganssle
    pganssle
    on Apr 26, 2023
    MemberAuthor
    More actions

    I've gone ahead and done some benchmarks for the most common legitimate use case I've seen for utcnow and utcfromtimestamp, which is getting a naïve datetime and immediately formatting it to a string with no time zone offset. The alternatives are considerably slower, though I don't know how important that is for real life use cases:

    >>> %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 http.cookiejar.time2isoz:

    >>> 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.

  3. pganssle
    added a commit that references this issue on Apr 27, 2023

    GH-103857: Deprecate utcnow and utcfromtimestamp (#103858)

    0b7fd8f
  4. pganssle commented on Apr 27, 2023

    @pganssle
    pganssle
    on Apr 27, 2023
    MemberAuthor
    More actions

    #103858 is now merged, but we still need a What's New entry before closing this, I believe.

  5. abalkin
    added a commit that references this issue on May 12, 2023

    gh-103857: Update deprecation stacktrace to point to calling line (#1…

    25db95d
  6. hugovk commented on May 16, 2023

    @hugovk
    hugovk
    on May 16, 2023
    Member
    More actions

    #103858 is now merged, but we still need a What's New entry before closing this, I believe.

    Please see PR #104542.

  7. hugovk
    added 2 commits that reference this issue on May 16, 2023

    Replace deprecated utcnow() with now(utc)\n\nRaises a deprecation war…

    24f959e

    gh-103857: Document utcnow and utcfromtimestamp deprecations in What'…

    5841fbc
  8. hugovk commented on May 22, 2023

    @hugovk
    hugovk
    on May 22, 2023
    Member
    More actions

    I think we're all set here! 🚀

  9. hugovk
    closed this as completedon May 22, 2023
  10. netbsd-srcmastr
    added a commit that references this issue on Jul 29, 2023

    py-mongo: updated to 4.4.1

    2749dcc
  11. jakelishman
    added a commit that references this issue on Sep 22, 2023

    Remove use of deprecated `datetime.datetime` methods

    0d45488
  12. Flotchet
    added a commit that references this issue on May 16, 2024

    [REF] hr*: remove the use of utcnow

    67ff28c
  13. robodoo
    added a commit that references this issue on May 30, 2024

    [REF] hr*: remove the use of utcnow

    bc08fb2
  14. 4 remaining items

  15. wantsui
    added a commit that references this issue on Jan 9, 2025

    chore: swap out deprecated datetime's utcnow and utcfromtimestamp (#1…

    bfa3b82
  16. hanwen-cluster
    added 2 commits that reference this issue on Jul 10, 2025

    Replace datetime.utcnow() with datetime.now(timezone.utc)

    4e4c061

    Replace datetime.utcnow() with datetime.now(timezone.utc)

    bf22eba
  17. msk
    added a commit that references this issue on May 11, 2026

    py-mongo: updated to 4.4.1

    1c948cc
  18. jperkin
    added a commit that references this issue on May 14, 2026

    py-mongo: updated to 4.4.1

    c8162c5
  19. copybara-service
    added 9 commits that reference this issue on May 22, 2026

    Fix flaky unit tests caused by mismatched timezones.

    8cccef1

    Fix flaky unit tests caused by mismatched timezones.

    bbd9865

    Fix flaky unit tests caused by mismatched timezones.

    4e1d613

    Fix flaky unit tests caused by mismatched timezones.

    7600741

    Fix flaky unit tests caused by mismatched timezones.

    f52b776

    Fix flaky unit tests caused by mismatched timezones.

    b701cc8

    Fix flaky unit tests caused by mismatched timezones.

    bdb702f

    Fix flaky unit tests caused by mismatched timezones.

    24f2bff

    Fix flaky unit tests caused by mismatched timezones.

    1f15f9d
  20. tsutsui
    added a commit that references this issue on Oct 17, 2026

    py-mongo: updated to 4.4.1

    dbad66a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.