# streaming_providers/base/utils/time_utils.py import re from typing import Optional # Pre-compile regex for ISO duration parsing ISO_8601_PERIOD_RE = re.compile( r"P(?:(?P\d+)Y)?(?:(?P\d+)M)?(?:(?P\d+)W)?(?:(?P\d+)D)?(?:T(?:(?P\d+)H)?(?:(?P\d+)M)?(?:(?P\d+(?:\.\d+)?)S)?)?" ) def parse_iso_duration(duration: str) -> int: """ Parse ISO 8601 duration string to seconds. Args: duration: ISO 8601 duration string (e.g., "PT30S", "PT2M30S") Returns: Total seconds as integer """ match = ISO_8601_PERIOD_RE.match(duration) if not match: return 0 d = match.groupdict() return int( int(d.get("hours") or 0) * 3600 + int(d.get("minutes") or 0) * 60 + float(d.get("seconds") or 0) )