from typing import Union
from datetime import datetime


def to_dt(s: Union[datetime, str]):
    """Convert an ISO 8601 string to a datetime."""
    if isinstance(s, str):
        try:
            return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%f")
        except ValueError:
            return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S")
    elif isinstance(s, datetime):
        return s