from datetime import datetime


def datetime_to_iso8601(timestamp: datetime) -> str:
    """Convert datetime to ISO 8601

    Args:
        timestamp (datetime): The timestamp

    Returns:
        str: The stringified ISO 8601 version of the timestamp
    """
    date_part = "{year:04d}-{month:02d}-{day:02d}".format(
        year=timestamp.year, month=timestamp.month, day=timestamp.day,
    )
    time_part = "{hour:02d}:{minute:02d}:{second:02d}.{millis:02d}".format(
        hour=timestamp.hour, minute=timestamp.minute, second=timestamp.second,
        millis=timestamp.microsecond // 1000
    )

    utcoffset = timestamp.utcoffset()
    if utcoffset is None:
        return f"{date_part}T{time_part}Z"
    else:
        tz_seconds = utcoffset.total_seconds()
        tz_sign = '-' if tz_seconds < 0 else '+'
        tz_minutes = int(abs(tz_seconds)) // 60
        tz_hours = tz_minutes // 60
        tz_minutes %= 60
        return f"{date_part}T{time_part}{tz_sign}{tz_hours:02d}:{tz_minutes:02d}"