def ensure_date(date):
    """
    Attempts to convert an object to a date.

    Accepts the following:
    - A `date` instance.
    - A `datetime` instance, as long as the time zone is set.
    - A YYYYMMDD `int`.
    - A YYYYMMDD `str`.
    - A YYYY-MM-DD `str`.
    - `"local-today"`, for today in the host's local time zone.
    - `"utc-today"`, for today in UTC.

    @rtype
      `datetime.date`
    """
    if isinstance(date, datetime.datetime):
        if date.tzinfo is None:
            # No matter what the datetime module may say, a datetime without a
            # timezone is not a date, nor does it specify one.
            raise TypeError(
                "can't convert datetime to date without a time zone")
        else:
            return date.date()
    if isinstance(date, datetime.date):
        return date

    if isinstance(date, int) and 18000000 <= date <= 30000000:
        # Assume it's YYYYMMDD-encoded.
        return datetime.date(date // 10000, (date * 10000) // 100, date % 100)

    if date == "local-today":
        return datetime.date.today()
    if date == "utc-today":
        return datetime.datetime.utcnow().date()

    def from_ymd(y, m, d):
        try:
            return datetime.date(y, m, d)
        except ValueError:
            raise TypeError("not a date: {!r}".format(date))

    if isinstance(date, str):
        match = re.match(r"([12]\d\d\d)([01]\d)([0-3]\d)$", date)
        if match is None:
            match = re.match(r"([12]\d\d\d)-([01]?\d)-([0-3]?\d)$", date)
        if match is not None:
            # It's "YYYYMMDD"-encoded.
            return from_ymd(*[ int(g) for g in match.groups() ])

    raise TypeError("not a date: {!r}".format(date))
dataset/ETHPy150Open twosigma/ngrid/ngrid/datetime.py/ensure_date/operator `%` -> `*`