日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

时间间隔输出函数 python3

發布時間:2023/12/14 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 时间间隔输出函数 python3 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

因為項目需要輸出指定時間間隔和間隔內模塊上報的次數,為了避免每次用計算器算,所以自己封裝了一個函數,以供自己使用:

import timedef time_unix(old_time):"""將格式為:'2020-9-18 17:50:00'的時間轉成unix表示的時間戳"""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):"""返回實際指定時間與現在的時間間隔old_t:代表以前的時間count:代表模塊上報周期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 '指定時間:【{}】距離現在【{}】 {:.2f}s'.format(old_t,now_time,end_t_s)if type == 'm':return '指定時間:【{}】距離現在【{}】 {:.2f}min'.format(old_t,now_time,end_t_s/60)if type == 'h':return '指定時間:【{}】距離現在【{}】 {:.2f}hour'.format(old_t,now_time,end_t_s/60/60)else:return '輸入有誤'else:end_t_c = end_t_s / 60 / countreturn '指定時間:【{}】距離現在【{}】 {:.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))

總結

以上是生活随笔為你收集整理的时间间隔输出函数 python3的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。