from typing import Optional
from datetime import datetime


def _date_converter(val: Optional[str]) -> Optional[datetime]:
    """Converts a datetime in RFC3339 format to datetime object."""
    if val is None:
        return None
    try:
        return datetime.strptime(val, "%Y-%m-%dT%H:%M:%S.%f%z")
    except ValueError:
        # Error will be raised if value does not match format
        # This mainly happens if date does not contain UTC offset
        return datetime.strptime(f"{val}Z", "%Y-%m-%dT%H:%M:%S.%f%z")