[dt] replace 'text.parse_datetime()' & 'text.parse_timestamp()'

This commit is contained in:
Mike Fährmann
2025-10-17 17:43:06 +02:00
parent d57dc48dcd
commit 085616e0a8
138 changed files with 220 additions and 260 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
@@ -322,46 +319,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