Skip to content

Commit

Permalink
Remove reference to utcnow
Browse files Browse the repository at this point in the history
This cleans up some of the datetime handling in the self check.

Note that this changes the format of the state file, since the datetime
now uses ``.isoformat()`` instead of ``.strftime``. Reading an outdated
state file will still work on Python 3.11+, but not on earlier versions.
  • Loading branch information
pganssle committed Apr 30, 2023
1 parent 28f77d7 commit 4c79131
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/pip/_internal/self_outdated_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from pip._internal.utils.filesystem import adjacent_tmp_file, check_path_owner, replace
from pip._internal.utils.misc import ensure_dir

_DATE_FMT = "%Y-%m-%dT%H:%M:%SZ"

_WEEK = datetime.timedelta(days=7)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,9 +76,9 @@ def get(self, current_time: datetime.datetime) -> Optional[str]:
seven_days_in_seconds = 7 * 24 * 60 * 60

# Determine if we need to refresh the state
last_check = datetime.datetime.strptime(self._state["last_check"], _DATE_FMT)
seconds_since_last_check = (current_time - last_check).total_seconds()
if seconds_since_last_check > seven_days_in_seconds:
last_check = datetime.datetime.fromisoformat(self._state["last_check"])
time_since_last_check = (current_time - last_check)
if time_since_last_check > _WEEK:
return None

return self._state["pypi_version"]
Expand All @@ -100,7 +100,7 @@ def set(self, pypi_version: str, current_time: datetime.datetime) -> None:
# Include the key so it's easy to tell which pip wrote the
# file.
"key": self.key,
"last_check": current_time.strftime(_DATE_FMT),
"last_check": current_time.isoformat(),
"pypi_version": pypi_version,
}

Expand Down Expand Up @@ -229,7 +229,7 @@ def pip_self_version_check(session: PipSession, options: optparse.Values) -> Non
try:
upgrade_prompt = _self_version_check_logic(
state=SelfCheckState(cache_dir=options.cache_dir),
current_time=datetime.datetime.utcnow(),
current_time=datetime.datetime.now(datetime.timezone.utc),
local_version=installed_dist.version,
get_remote_version=functools.partial(
_get_current_remote_pip_version, session, options
Expand Down

0 comments on commit 4c79131

Please sign in to comment.