时间间隔输出函数 python3
生活随笔
收集整理的這篇文章主要介紹了
时间间隔输出函数 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 秒懂mysql中的group by用法
- 下一篇: 【java校招你不知道的那些事儿】jav