Fork me on GitHub

一 概念
datetime模块属性如下所示:
 
二 源码解析
该源码中,对时间库datetime应用十分到位,可以看作如下所示:
import datetime
import threading

def func():
    print("start rec audio")
    timer = threading.Timer(86400,func)
    timer.start()


now_time = datetime.datetime.now()

next_time = now_time + datetime.timedelta(days=+1)
next_year = next_time.date().year
next_month = next_time.date().month
next_day = next_time.date().day

#get next day 3:00 time
#next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+"3:00:00"+"%Y-%m-%d %H:%M:%S")

next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 03:00:00", "%Y-%m-%d %H:%M:%S")

timer_start_time = (next_time - now_time).total_seconds()
print(timer_start_time)

timer = threading.Timer(timer_start_time,func)
timer.start()

 

 
三 总结
这个是对datetime的运用全面范例,值得深究。
 
四 参考文档
posted on 2022-05-02 17:45  虚生  阅读(614)  评论(0编辑  收藏  举报