[dt] replace 'parse_compat' with compat workaround

This commit is contained in:
Mike Fährmann
2025-10-19 18:26:32 +02:00
parent 6c71b279b6
commit 989136bc59
3 changed files with 16 additions and 33 deletions

View File

@@ -38,19 +38,10 @@ def convert(value):
"""Convert 'value' to a naive UTC datetime object"""
if not value:
return NONE
if isinstance(value, datetime):
return value
if isinstance(value, str):
try:
if value[-1] == "Z":
# compat for Python < 3.11
value = value[:-1]
return normalize(datetime.fromisoformat(value))
except Exception:
pass
return normalize(value)
if isinstance(value, str) and (dt := parse_iso(value)) is not NONE:
return dt
return parse_ts(value)
@@ -70,14 +61,13 @@ if sys.hexversion < 0x30c0000:
if dt_string[-1] == "Z":
# compat for Python < 3.11
dt_string = dt_string[:-1]
elif dt_string[-5] in "+-":
# compat for Python < 3.11
dt_string = f"{dt_string[:-2]}:{dt_string[-2:]}"
return normalize(datetime.fromisoformat(dt_string))
except Exception:
return NONE
def parse_compat(dt_string, format="%Y-%m-%dT%H:%M:%S%z"):
"""Parse 'dt_string' as ISO 8601 value using 'format'"""
return parse(dt_string, format)
from_ts = datetime.utcfromtimestamp
now = datetime.utcnow
@@ -90,10 +80,6 @@ else:
except Exception:
return NONE
def parse_compat(dt_string, format=None):
"""Parse 'dt_string' as ISO 8601 value"""
return parse_iso(dt_string)
def from_ts(ts=None):
"""Convert Unix timestamp to naive UTC datetime"""
Y, m, d, H, M, S, _, _, _ = time.gmtime(ts)

View File

@@ -218,7 +218,7 @@ class NewgroundsExtractor(Extractor):
"description": text.unescape(extr(':description" content="', '"')),
"type" : "art",
"_type" : "i",
"date" : dt.parse_compat(extr(
"date" : dt.parse_iso(extr(
'itemprop="datePublished" content="', '"')),
"rating" : extr('class="rated-', '"'),
"url" : full('src="', '"'),
@@ -268,7 +268,7 @@ class NewgroundsExtractor(Extractor):
"description": text.unescape(extr(':description" content="', '"')),
"type" : "audio",
"_type" : "a",
"date" : dt.parse_compat(extr(
"date" : dt.parse_iso(extr(
'itemprop="datePublished" content="', '"')),
"url" : extr('{"url":"', '"').replace("\\/", "/"),
"index" : text.parse_int(index),
@@ -287,7 +287,7 @@ class NewgroundsExtractor(Extractor):
src = src.replace("\\/", "/")
formats = ()
type = extr(',"description":"', '"')
date = dt.parse_compat(extr(
date = dt.parse_iso(extr(
'itemprop="datePublished" content="', '"'))
if type:
type = type.rpartition(" ")[2].lower()

View File

@@ -43,6 +43,8 @@ class TestDatetime(unittest.TestCase):
_assert("2009-12-31T19:00:00.123456-05:00", d)
_assert("2010-01-01T00:00:00Z" , d)
_assert("2010-01-01T00:00:00.123456Z" , d)
_assert("2009-12-31T19:00:00-0500" , d)
_assert("2009-12-31T19:00:00.123456-0500" , d)
_assert(0 , dt.NONE)
_assert("" , dt.NONE)
@@ -63,11 +65,8 @@ class TestDatetime(unittest.TestCase):
self.assertEqual(result, expected, msg=repr(value))
d = datetime.datetime(2010, 1, 1)
_assert("2009-12-31T19:00:00-05" , d)
_assert("2009-12-31T19:00:00-0500" , d)
_assert("2009-12-31T19:00:00.123456-05" , d)
_assert("2009-12-31T19:00:00.123456-0500" , d)
def test_to_timestamp(self, f=dt.to_ts):
self.assertEqual(f(dt.EPOCH), 0.0)
@@ -146,20 +145,18 @@ class TestDatetime(unittest.TestCase):
f("1970.01.01"),
dt.NONE,
)
for value in ((), [], {}, None, 1, 2.3):
self.assertEqual(f(value), dt.NONE)
def test_parse_compat(self, f=dt.parse_compat):
self.assertEqual(
f("1970-01-01T00:00:00+0000", "%Y-%m-%dT%H:%M:%S%z"),
f("1970-01-01T00:00:00+0000"),
dt.EPOCH,
)
self.assertEqual(
f("2019-05-07T21:25:02.753+0900", "%Y-%m-%dT%H:%M:%S.%f%z"),
f("2019-05-07T21:25:02.753+0900"),
datetime.datetime(2019, 5, 7, 12, 25, 2),
)
for value in ((), [], {}, None, 1, 2.3):
self.assertEqual(f(value), dt.NONE)
def test_none(self):
self.assertFalse(dt.NONE)
self.assertIsInstance(dt.NONE, dt.datetime)