core/compat: move fromisoformat to .core.compat module
This commit is contained in:
parent
09e0f66892
commit
a843407e40
8 changed files with 69 additions and 34 deletions
|
@ -76,3 +76,42 @@ if sys.version_info[:2] <= (3, 9):
|
|||
return lo
|
||||
else:
|
||||
from bisect import bisect_left
|
||||
|
||||
|
||||
from datetime import datetime
|
||||
if sys.version_info[:2] >= (3, 11):
|
||||
fromisoformat = datetime.fromisoformat
|
||||
else:
|
||||
def fromisoformat(date_string: str) -> datetime:
|
||||
# didn't support Z as "utc" before 3.11
|
||||
if date_string.endswith('Z'):
|
||||
# NOTE: can be removed from 3.11?
|
||||
# https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat
|
||||
date_string = date_string[:-1] + '+00:00'
|
||||
return datetime.fromisoformat(date_string)
|
||||
|
||||
|
||||
def test_fromisoformat() -> None:
|
||||
from datetime import timezone
|
||||
|
||||
# feedbin has this format
|
||||
assert fromisoformat('2020-05-01T10:32:02.925961Z') == datetime(
|
||||
2020, 5, 1, 10, 32, 2, 925961, timezone.utc,
|
||||
)
|
||||
|
||||
# polar has this format
|
||||
assert fromisoformat('2018-11-28T22:04:01.304Z') == datetime(
|
||||
2018, 11, 28, 22, 4, 1, 304000, timezone.utc,
|
||||
)
|
||||
|
||||
# stackexchange, runnerup has this format
|
||||
assert fromisoformat('2020-11-30T00:53:12Z') == datetime(
|
||||
2020, 11, 30, 0, 53, 12, 0, timezone.utc,
|
||||
)
|
||||
|
||||
# arbtt has this format (sometimes less/more than 6 digits in milliseconds)
|
||||
# TODO doesn't work atm, not sure if really should be supported...
|
||||
# maybe should have flags for weird formats?
|
||||
# assert isoparse('2017-07-18T18:59:38.21731Z') == datetime(
|
||||
# 2017, 7, 18, 18, 59, 38, 217310, timezone.utc,
|
||||
# )
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue