def to_datetime(value):
    """转化为 datetime.datetime 类型"""
    if not value:
        return value
    elif isinstance(value, six.string_types):
        try:
            slen = len(value)
            if slen == 12 or slen == 14:  # 202101010000 or 20210101000000
                return datetime.datetime(
                    int(value[:4]),
                    *map(int, (value[idx:(idx + 2)] for idx in range(4, slen, 2)))
                )
            else:
                return datetime.datetime(*map(int, re.split(r"\W+", value)))
        except Exception:
            return datetime.datetime.strptime(str(to_date(value)), '%Y-%m-%d')
    elif isinstance(value, datetime.datetime):
        return value
    elif isinstance(value, datetime.date):
        return datetime.datetime.combine(value, datetime.time.min)
    elif isinstance(value, bytes):
        return to_datetime(value.decode('utf8'))
    raise ValueError("无效的日期：{!r}".format(value))
转化为 datetime.datetime 类型
jqdatasdk/utils.py