🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## time时间获取和转换 ## 获取时间 ### time.ctime() 当前时间 字符串 ~~~ time.ctime() Out[116]: 'Wed May 31 15:27:40 2017' ~~~ ### time.time() 当前时间戳 ### time.gmtime([secs]) 计算机可以处理的字符串 ~~~ Out[10]: time.struct_time(tm_year=2018, tm_mon=6, tm_mday=2, tm_hour=11, tm_min=33, tm_sec=27, tm_wday=5, tm_yday=153, tm_isdst=0) ~~~ ### time.localtime([secs]) 类似gmtime() 但是转换成当地时间,从时间元年开始 返回当前时间前100秒的时间 ~~~ time.localtime(time.time()-100) ~~~ ## 格式化时间 ### time.strftime(format[, t]) 将元组或者gmtime()或者localtime()返回的表示时间的struct_time 转换为由参数format指定的字符串。如果没有提供t,则使用localtime() 返回的当前时间。 ~~~ strftime(tpl, ts) In [11]: t = time.gmtime() In [14]: time.strftime("%Y-%m-%d %H:%M:%S", t) Out[14]: '2018-06-02 11:36:18' ~~~ ### time.strptime(string[, format]) 根据格式解析一个表示时间的字符串。返回值是struct_time ,和gmtime()或localtime()的返回值一样。 format参数使用的指令和strftime()使用的一样;默认为`"%a %b %d %H:%M:%S %Y"` ,与ctime()返回的格式匹配。如果string不能根据 format解析出来,或者解析之后又多余的数据,那么将会抛出ValueError异常。当不能推测出准确的值时,用于填充缺失的数据的默认值为 (1900, 1, 1, 0, 0, 0, 0, 1, -1)。 ![](https://ws1.sinaimg.cn/large/006tKfTcgy1frxseyybtjj31ho0kmavu.jpg) ![](https://ws1.sinaimg.cn/large/006tKfTcgy1frxsfdvufaj31g20ki7n5.jpg) ## 计时 ~~~ time.sleep() time.perf_counter() 获取计算机 CPU 级别的时间计数值,差值有意义 ~~~ ### 进度条函数 ![](https://ws4.sinaimg.cn/large/006tKfTcgy1frxsijoxpwj315y0mue20.jpg)