Skip to content

Commit a53d08a

Browse files
committed
Make common parse_iso_time
1 parent c8b2481 commit a53d08a

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/pip/_internal/models/link.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
import posixpath
99
import re
1010
import urllib.parse
11+
import urllib.request
1112
from collections.abc import Mapping
1213
from dataclasses import dataclass
13-
from typing import (
14-
TYPE_CHECKING,
15-
Any,
16-
NamedTuple,
17-
)
14+
from typing import TYPE_CHECKING, Any, NamedTuple
1815

16+
from pip._internal.utils.datetime import parse_iso_datetime
1917
from pip._internal.utils.deprecation import deprecated
2018
from pip._internal.utils.filetypes import WHEEL_EXTENSION
2119
from pip._internal.utils.hashes import Hashes
@@ -216,11 +214,11 @@ class Link:
216214
def __init__(
217215
self,
218216
url: str,
219-
comes_from: Optional[Union[str, "IndexContent"]] = None,
220-
requires_python: Optional[str] = None,
221-
yanked_reason: Optional[str] = None,
222-
metadata_file_data: Optional[MetadataFile] = None,
223-
upload_time: Optional[datetime.datetime] = None,
217+
comes_from: str | IndexContent | None = None,
218+
requires_python: str | None = None,
219+
yanked_reason: str | None = None,
220+
metadata_file_data: MetadataFile | None = None,
221+
upload_time: datetime.datetime | None = None,
224222
cache_link_parsing: bool = True,
225223
hashes: Mapping[str, str] | None = None,
226224
) -> None:
@@ -306,9 +304,9 @@ def from_json(
306304
if metadata_info is None:
307305
metadata_info = file_data.get("dist-info-metadata")
308306

309-
upload_time: Optional[datetime.datetime]
307+
upload_time: datetime.datetime | None
310308
if upload_time_data := file_data.get("upload-time"):
311-
upload_time = datetime.datetime.fromisoformat(upload_time_data)
309+
upload_time = parse_iso_datetime(upload_time_data)
312310
else:
313311
upload_time = None
314312

src/pip/_internal/self_outdated_check.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from pip._internal.models.selection_prefs import SelectionPreferences
2424
from pip._internal.network.session import PipSession
2525
from pip._internal.utils.compat import WINDOWS
26+
from pip._internal.utils.datetime import parse_iso_datetime
2627
from pip._internal.utils.entrypoints import (
2728
get_best_invocation_for_this_pip,
2829
get_best_invocation_for_this_python,
@@ -45,15 +46,6 @@ def _get_statefile_name(key: str) -> str:
4546
return name
4647

4748

48-
def _convert_date(isodate: str) -> datetime.datetime:
49-
"""Convert an ISO format string to a date.
50-
51-
Handles the format 2020-01-22T14:24:01Z (trailing Z)
52-
which is not supported by older versions of fromisoformat.
53-
"""
54-
return datetime.datetime.fromisoformat(isodate.replace("Z", "+00:00"))
55-
56-
5749
class SelfCheckState:
5850
def __init__(self, cache_dir: str) -> None:
5951
self._state: dict[str, Any] = {}
@@ -88,7 +80,7 @@ def get(self, current_time: datetime.datetime) -> str | None:
8880
return None
8981

9082
# Determine if we need to refresh the state
91-
last_check = _convert_date(self._state["last_check"])
83+
last_check = parse_iso_datetime(self._state["last_check"])
9284
time_since_last_check = current_time - last_check
9385
if time_since_last_check > _WEEK:
9486
return None

src/pip/_internal/utils/datetime.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
"""For when pip wants to check the date or time."""
22

33
import datetime
4+
import sys
45

56

67
def today_is_later_than(year: int, month: int, day: int) -> bool:
78
today = datetime.date.today()
89
given = datetime.date(year, month, day)
910

1011
return today > given
12+
13+
14+
def parse_iso_datetime(isodate: str) -> datetime.datetime:
15+
"""Convert an ISO format string to a datetime.
16+
17+
Handles the format 2020-01-22T14:24:01Z (trailing Z)
18+
which is not supported by older versions of fromisoformat.
19+
"""
20+
# Python 3.11+ supports Z suffix natively in fromisoformat
21+
if sys.version_info >= (3, 11):
22+
return datetime.datetime.fromisoformat(isodate)
23+
else:
24+
return datetime.datetime.fromisoformat(isodate.replace("Z", "+00:00"))

0 commit comments

Comments
 (0)