from datetime import datetime


def fromisoformat(date_string):
    """Construct a datetime object from an ISO 8601 string.

    Parameters
    ----------
    date_string : :class:`str`
        The string representation of the date and time.

    Returns
    -------
    :class:`datetime.datetime`
        The datetime object.
    """
    try:
        # datetime.fromisoformat is available in Python 3.7+
        return datetime.fromisoformat(date_string)
    except AttributeError:
        dstr = date_string[0:10]
        tstr = date_string[11:]
        date = datetime.strptime(dstr, '%Y-%m-%d')
        if not tstr:
            return date

        # Parses times of the form HH[:MM[:SS]]
        hour, minute, second = 0, 0, 0
        time_split = tuple(map(int, tstr.split(':')))
        if len(time_split) == 1:
            hour = time_split[0]
        elif len(time_split) == 2:
            hour, minute = time_split
        elif len(time_split) == 3:
            hour, minute, second = time_split

        return datetime(date.year, month=date.month, day=date.day,
                        hour=hour, minute=minute, second=second)