编程语言
首页 > 编程语言> > 更快的datetime string to python datetime转换模块 ciso8601

更快的datetime string to python datetime转换模块 ciso8601

作者:互联网

https://github.com/closeio/ciso8601

 

ciso8601 converts ISO 8601 or RFC 3339 date time strings into Python datetime objects.

Since it's written as a C module, it is much faster than other Python libraries. Tested with Python 2.7, 3.4, 3.5, 3.6, 3.7.

 

 

当然,如果格式固定已知,那么从字符串里拆出指定位数的字符再转化为int也不算太慢

https://www.peterbe.com/plog/fastest-python-datetime-parser

 

def f1(datestr):
    return datetime.datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%S.%fZ')


def f2(datestr):
    return ciso8601.parse_datetime(datestr)


def f3(datestr):
    return datetime.datetime(
        int(datestr[:4]),
        int(datestr[5:7]),
        int(datestr[8:10]),
        int(datestr[11:13]),
        int(datestr[14:16]),
        int(datestr[17:19]),
    )

标签:Python,return,string,python,datetime,int,datestr,faster
来源: https://www.cnblogs.com/yeyong/p/10441507.html