

def ensure_datetime(dt):
    '\n    Attempts to convert an object to a UTC datetime.\n\n    Accepts the following:\n    - A `datetime` instance.\n    - A `numpy.datetime64` instance.\n    - `"now"`, for the current datetime.\n    - A "YYYY-MM-DD HH:MM:SS" string, assumed to be UTC.\n    - A "YYYY-MM-DDTHH:MM:SSZ" ISO 8601 string.\n    '
    if isinstance(dt, datetime.datetime):
        return dt
    try:
        item = dt.item()
    except:
        pass
    else:
        if isinstance(item, datetime.datetime):
            return item.replace(tzinfo=UTC)
    if (dt == 'now'):
        return datetime.datetime.utcnow().replace(tzinfo=UTC)

    def from_parts(ye, mo, da, ho=0, mi=0, se=0):
        try:
            return datetime.datetime(ye, mo, da, ho, mi, se, tzinfo=UTC)
        except ValueError:
            raise TypeError('not a datetime: {!r}'.format(dt))
    if isinstance(dt, str):
        for regex in _DATETIME_REGEXES:
            match = regex.match(dt)
            if (match is not None):
                ye = int(match.group('ye'))
                mo = int(match.group('mo'))
                da = int(match.group('da'))
                ho = int(if_none(match.group('ho'), 0))
                mi = int(if_none(match.group('mi'), 0))
                se = int(if_none(match.group('se'), 0))
                return from_parts(ye, mo, da, ho, mi, se)
    raise TypeError('not a datetime: {!r}'.format(dt))

dataset/ETHPy150Open twosigma/ngrid/ngrid/datetime.py ensure_datetime/original