def parse_date_optional_time()

in elastic/shared/utils/time.py [0:0]


def parse_date_optional_time(date_time: str):
    match = re.match(
        r"^(\d{4})\D(\d{2})\D(\d{2})\D(\d{2})\D(\d{2})\D(\d{2})(?:\.\d{0,3})?Z?$",
        date_time,
    )
    if match:
        return datetime(
            year=int(match.group(1)),
            month=int(match.group(2)),
            day=int(match.group(3)),
            hour=int(match.group(4)),
            minute=int(match.group(5)),
            second=int(match.group(6)),
            tzinfo=timezone.utc,
        )
    else:
        match = re.match(r"^(\d{4})\D(\d{2})\D(\d{2})$", date_time)
        if match:
            return datetime(
                year=int(match.group(1)),
                month=int(match.group(2)),
                day=int(match.group(3)),
                tzinfo=timezone.utc,
            )
    raise TimeParsingError(f"Invalid time format: {date_time}")