merge branch 'dt': move datetime utils into separate module

- use 'datetime.fromisoformat()' when possible (#7671)
- return a datetime-compatible object for invalid datetimes
  (instead of a 'str' value)
This commit is contained in:
Mike Fährmann
2025-10-20 09:30:05 +02:00
177 changed files with 652 additions and 708 deletions

View File

@@ -8,10 +8,7 @@
"""Collection of functions that work on strings/text"""
import sys
import html
import time
import datetime
import urllib.parse
import re as re_module
@@ -340,46 +337,6 @@ def build_query(params):
])
if sys.hexversion < 0x30c0000:
# Python <= 3.11
def parse_timestamp(ts, default=None):
"""Create a datetime object from a Unix timestamp"""
try:
return datetime.datetime.utcfromtimestamp(int(ts))
except Exception:
return default
else:
# Python >= 3.12
def parse_timestamp(ts, default=None):
"""Create a datetime object from a Unix timestamp"""
try:
Y, m, d, H, M, S, _, _, _ = time.gmtime(int(ts))
return datetime.datetime(Y, m, d, H, M, S)
except Exception:
return default
def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z", utcoffset=0):
"""Create a datetime object by parsing 'date_string'"""
try:
d = datetime.datetime.strptime(date_string, format)
o = d.utcoffset()
if o is not None:
# convert to naive UTC
d = d.replace(tzinfo=None, microsecond=0) - o
else:
if d.microsecond:
d = d.replace(microsecond=0)
if utcoffset:
# apply manual UTC offset
d += datetime.timedelta(0, utcoffset * -3600)
return d
except (TypeError, IndexError, KeyError):
return None
except (ValueError, OverflowError):
return date_string
urljoin = urllib.parse.urljoin
quote = urllib.parse.quote