def ensure_time(time):
    """
    Attempts to convert an object to a time (of day).

    @rtype
      `datetime.time`
    """
    if isinstance(time, datetime.datetime):
        return date.time()
    if isinstance(time, datetime.time):
        return time

    if time == "local-now":
        return datetime.datetime.now().time()
    if time == "utc-now":
        return datetime.datetime.utcnow().time()

    def from_parts(h, m, s=0):
        try:
            return datetime.time(h, m, s)
        except ValueError:
            raise TypeError("not a time: {!r}".format(time))

    if isinstance(time, str):
        match = re.match(r"(\d?\d):(\d\d):(\d\d)", time)
        if match is None:
            match = re.match(r"(\d?\d):(\d\d)", time)
        if match is not None:
            return from_parts(*[ int(g) for g in match.groups() ])

    raise TypeError("not a time: {!r}".format(time))
dataset/ETHPy150Open twosigma/ngrid/ngrid/datetime.py/ensure_time/original