Convert 8601 (ISO) date string to datetime object.

    Handles "Z" and milliseconds transparently.

    :param date_str: Date string.
    :type  date_str: ``string``
    :return: Date time.
    :rtype:  :class:`datetime.datetime`

def dt_from_rfc8601(date_str):
    """Convert 8601 (ISO) date string to datetime object.

    Handles "Z" and milliseconds transparently.

    :param date_str: Date string.
    :type  date_str: ``string``
    :return: Date time.
    :rtype:  :class:`datetime.datetime`
    """
    # Normalize string and adjust for milliseconds. Note that Python 2.6+ has
    # ".%f" format, but we're going for Python 2.5, so truncate the portion.
    date_str = date_str.rstrip('Z').split('.')[0]

    # Format string. (2010-04-13T14:02:48.000Z)
    fmt = "%Y-%m-%dT%H:%M:%S"
    # Python 2.6+: Could format and handle milliseconds.
    # if date_str.find('.') >= 0:
    #    fmt += ".%f"

    return datetime.strptime(date_str, fmt)