时间间隔输出函数 python3
生活随笔
收集整理的這篇文章主要介紹了
时间间隔输出函数 python3
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
因?yàn)轫?xiàng)目需要輸出指定時(shí)間間隔和間隔內(nèi)模塊上報(bào)的次數(shù),為了避免每次用計(jì)算器算,所以自己封裝了一個(gè)函數(shù),以供自己使用:
import timedef time_unix(old_time):"""將格式為:'2020-9-18 17:50:00'的時(shí)間轉(zhuǎn)成unix表示的時(shí)間戳"""data_sj = time.strptime(old_time,'%Y-%m-%d %H:%M:%S')time_int = int(time.mktime(data_sj))return time_intdef count_t(old_t,type,count = 15):"""返回實(shí)際指定時(shí)間與現(xiàn)在的時(shí)間間隔old_t:代表以前的時(shí)間count:代表模塊上報(bào)周期type:代表類型,值為:‘s’,'m','h'"""now_t = time.time()now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(time.time())))end_t_s = now_t - time_unix(old_t)if count == 0:if type == 's':return '指定時(shí)間:【{}】距離現(xiàn)在【{}】 {:.2f}s'.format(old_t,now_time,end_t_s)if type == 'm':return '指定時(shí)間:【{}】距離現(xiàn)在【{}】 {:.2f}min'.format(old_t,now_time,end_t_s/60)if type == 'h':return '指定時(shí)間:【{}】距離現(xiàn)在【{}】 {:.2f}hour'.format(old_t,now_time,end_t_s/60/60)else:return '輸入有誤'else:end_t_c = end_t_s / 60 / countreturn '指定時(shí)間:【{}】距離現(xiàn)在【{}】 {:.2f}s,以{}min為間隔,共{}次'.format(old_t,now_time,end_t_s,count,end_t_c)if __name__ == '__main__':test1 = '2020-9-18 17:50:00'print(count_t(test1, 'd', 15))print(count_t(test1, 'h',0))總結(jié)
以上是生活随笔為你收集整理的时间间隔输出函数 python3的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 秒懂mysql中的group by用法
- 下一篇: 【java校招你不知道的那些事儿】jav