import re
from datetime import datetime


def parseDateString(dateString):
    """ Function to parse a dateString compliant to ISO 8601 profile specified
        by W3CDTF (https://www.w3.org/TR/NOTE-datetime, retrieved 2020-03-09)
        or None otherwise

    Argument
    --------
    dateString: str
        String representing a valid date format

    Returns
    -------
    Datetime object
    """
    # Remedy for "+/-%H:%S" time zone formatting
    # adapted version from https://stackoverflow.com/a/45300534
    # Should become superfluous if Python version is >= 3.7
    if re.match(r".*(\+|-)\d{1,2}:\d\d$", dateString):
        # drop the colon
        dateString = dateString[:-3] + dateString[-2:]
    formats = ("%Y", "%Y-%m", "%Y-%m-%d", "%Y-%m-%dT%H:%M%z", "%Y-%m-%dT%H:%M:%S%z")
    for f in formats:
        try:
            return datetime.strptime(dateString, f)
        except ValueError:
            continue
    raise ValueError("'{}' is not in a supported format".format(dateString))