Skip to content

Commit

Permalink
Do not use datetime.utcnow() that is deprecated in Python 3.12 (#972)
Browse files Browse the repository at this point in the history
* Do not use datetime.utcnow() that is deprecated in Python 3.12

Import session.utcnow() into utils instead of reimplementing it

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove unneeded noqa

* Add noqa

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: David Brochart <david.brochart@gmail.com>
Co-authored-by: Steven Silvester <steven.silvester@ieee.org>
  • Loading branch information
4 people authored Sep 14, 2023
1 parent fe93a44 commit 60c1095
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 34 deletions.
2 changes: 1 addition & 1 deletion jupyter_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def default_secure(cfg: t.Any) -> None: # pragma: no cover

def utcnow() -> datetime:
"""Return timezone-aware UTC timestamp"""
return datetime.utcnow().replace(tzinfo=utc) # noqa
return datetime.now(utc)


# -----------------------------------------------------------------------------
Expand Down
35 changes: 2 additions & 33 deletions jupyter_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
- vendor functions from ipython_genutils that should be retired at some point.
"""
import os
from datetime import datetime, timedelta, tzinfo

from jupyter_core.utils import ensure_async, run_sync # noqa: F401 # noqa: F401

from .session import utcnow # noqa


def _filefind(filename, path_dirs=None):
"""Find a file by looking through a sequence of paths.
Expand Down Expand Up @@ -84,35 +85,3 @@ def _expand_path(s):
if os.name == "nt":
s = s.replace("IPYTHON_TEMP", "$\\")
return s


# constant for zero offset
ZERO = timedelta(0)


class tzUTC(tzinfo): # noqa
"""tzinfo object for UTC (zero offset)"""

def utcoffset(self, d):
"""Compute utcoffset."""
return ZERO

def dst(self, d):
"""Compute dst."""
return ZERO


UTC = tzUTC() # type:ignore[abstract]


def utc_aware(unaware):
"""decorator for adding UTC tzinfo to datetime's utcfoo methods"""

def utc_method(*args, **kwargs):
dt = unaware(*args, **kwargs)
return dt.replace(tzinfo=UTC)

return utc_method


utcnow = utc_aware(datetime.utcnow)

0 comments on commit 60c1095

Please sign in to comment.