jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1353439?usp=email )

Change subject: time: Speed up parsing of API ISO timestamps
......................................................................

time: Speed up parsing of API ISO timestamps

Use fromisoformat() for the ASCII YYYY-MM-DDTHH:MM:SSZ timestamps
returned by the API. Strip the UTC suffix to retain naive Timestamp
results and avoid strptime() and timezone conversion for this case.

Apply the fast path in the shared ISO parser so fromISOformat() and
set_timestamp() both benefit. Keep the existing parser for other forms
and respect the caller's allowed separators.

Change-Id: I4e194b3b5b96c09ef4664c04e86d76460b5b93c9
---
M pywikibot/time.py
M tests/time_tests.py
2 files changed, 11 insertions(+), 0 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/time.py b/pywikibot/time.py
index 4b3497e..97ff6d6 100644
--- a/pywikibot/time.py
+++ b/pywikibot/time.py
@@ -46,6 +46,9 @@
 .. version-added:: 11.9
 """

+_API_TIMESTAMP = re.compile(
+    r'\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z', re.ASCII)
+

 class Timestamp(datetime.datetime):

@@ -150,6 +153,10 @@

         .. version-added:: 7.5
         """
+        if 'T' in separators and _API_TIMESTAMP.fullmatch(timestr):
+            # API timestamps are already UTC; keep the result naive.
+            return cls.fromisoformat(timestr.removesuffix('Z'))
+
         RE_ISO8601 = (rf'(?:\d{{4}}-\d{{2}}-\d{{2}})'  # noqa: N806
                       rf'(?P<sep>[{re.escape(separators)}])'
                       r'(?:\d{2}:\d{2}:\d{2})(?P<u>[.,]\d{1,6})?'
diff --git a/tests/time_tests.py b/tests/time_tests.py
index af53b25..91d7b63 100755
--- a/tests/time_tests.py
+++ b/tests/time_tests.py
@@ -63,6 +63,7 @@
         'INVALID': [
             ['200902132331309999', None],
             ['2009-99-99 22:31:30.123456-01', None],
+            ['2009-02-13T23:31:30+00:00Z', None],
             ['1234567890.1234569999', None],
         ],
     }
@@ -177,6 +178,9 @@
         self.assertEqual(date, str(t1.date()))
         self.assertEqual(time, str(t1.time()))

+        with self.assertRaisesRegex(ValueError, 'does not match ISO8601'):
+            Timestamp.fromISOformat(t1.isoformat(), sep=sep)
+
     def test_iso_format_property(self) -> None:
         """Test iso format properties."""
         with suppress_warnings(

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1353439?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I4e194b3b5b96c09ef4664c04e86d76460b5b93c9
Gerrit-Change-Number: 1353439
Gerrit-PatchSet: 3
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to