Python3 - 时间处理与定时任务

来源:互联网 发布:单片机setb 编辑:程序博客网 时间:2024/06/05 07:54

无论哪种编程语言,时间肯定都是非常重要的部分,今天来看一下python如何来处理时间和python定时任务,注意咯:本篇所讲是python3版本的实现,在python2版本中的实现略有不同,有时间会再写一篇以便大家区分。

文章转自:pythontab中文网 - python中文开发者社区门户

1.计算明天和昨天的日期

[python] view plaincopyprint?
  1. # 获得今天的日期,并计算昨天和明天的日期  
  2. import datetime  
  3.   
  4. today = datetime.date.today()  
  5. yesterday = today - datetime.timedelta(days = 1)  
  6. tomorrow = today + datetime.timedelta(days = 1)  
  7.   
  8. print(yesterday, today, tomorrow)  

2.寻找上一个星期五

[python] view plaincopyprint?
  1. # 寻找上一个星期五  
  2. import datetime  
  3. import calendar  
  4.   
  5. last_friday = datetime.date.today()  
  6. oneday = datetime.timedelta(days = 1)  
  7.   
  8. while last_friday.weekday() != calendar.FRIDAY:  
  9.     last_friday -= oneday  
  10.   
  11. print(last_friday.strftime('%A, %d-%b-%Y'))  


3.借助模运算寻找上一个星期五

[python] view plaincopyprint?
  1. # 借助模运算,可以一次算出需要减去的天数,寻找上一个星期五  
  2. import datetime  
  3. import calendar  
  4.   
  5. today = datetime.date.today()  
  6. target_day = calendar.FRIDAY  
  7. this_day = today.weekday()  
  8. delta_to_target = (this_day - target_day) % 7  
  9. last_friday = today - datetime.timedelta(days = delta_to_target)  
  10.   
  11. print(last_friday.strftime("%d-%b-%Y"))  


4.计算歌曲的总播放时间

[python] view plaincopyprint?
  1. # 想获取一个列表中的所有歌曲的播放时间之和  
  2. import datetime  
  3.   
  4. def total_timer(times):  
  5.     td = datetime.timedelta(0)  
  6.     duration = sum([datetime.timedelta(minutes = m, seconds = s) for m, s in times], td)  
  7.     return duration  
  8.   
  9. times1 = [(236),  
  10.           (335),  
  11.           (345),  
  12.           ]  
  13. times2 = [(30),  
  14.           (513),  
  15.           (412),  
  16.           (110),  
  17.           ]  
  18.   
  19. assert total_timer(times1) == datetime.timedelta(0596)  
  20. assert total_timer(times2) == datetime.timedelta(0815)  
  21.   
  22. print("Tests passed.\n"  
  23.       "First test total: %s\n"  
  24.       "Second test total: %s" % (total_timer(times1), total_timer(times2)))  


5.反复执行某个命令

[python] view plaincopyprint?
  1. # 以需要的时间间隔执行某个命令  
  2.   
  3. import time, os  
  4.   
  5. def re_exe(cmd, inc = 60):  
  6.     while True:  
  7.         os.system(cmd);  
  8.         time.sleep(inc)  
  9.   
  10. re_exe("echo %time%"5)  

6.定时任务

[python] view plaincopyprint?
  1. import time, os, sched  
  2.   
  3. # 第一个参数确定任务的时间,返回从某个特定的时间到现在经历的秒数  
  4. # 第二个参数以某种人为的方式衡量时间  
  5. schedule = sched.scheduler(time.time, time.sleep)  
  6.   
  7. def perform_command(cmd, inc):  
  8.     os.system(cmd)  
  9.       
  10. def timming_exe(cmd, inc = 60):  
  11.     # enter用来安排某事件的发生时间,从现在起第n秒开始启动  
  12.     schedule.enter(inc, 0, perform_command, (cmd, inc))  
  13.     # 持续运行,直到计划时间队列变成空为止  
  14.     schedule.run()  
  15.       
  16.   
  17. print("show time after 10 seconds:")  
  18. timming_exe("echo %time%"10)  


7.利用sched实现周期调用

[python] view plaincopyprint?
  1. import time, os, sched  
  2.   
  3. # 第一个参数确定任务的时间,返回从某个特定的时间到现在经历的秒数  
  4. # 第二个参数以某种人为的方式衡量时间  
  5. schedule = sched.scheduler(time.time, time.sleep)  
  6.   
  7. def perform_command(cmd, inc):  
  8.     # 安排inc秒后再次运行自己,即周期运行  
  9.     schedule.enter(inc, 0, perform_command, (cmd, inc))  
  10.     os.system(cmd)  
  11.       
  12. def timming_exe(cmd, inc = 60):  
  13.     # enter用来安排某事件的发生时间,从现在起第n秒开始启动  
  14.     schedule.enter(inc, 0, perform_command, (cmd, inc))  
  15.     # 持续运行,直到计划时间队列变成空为止  
  16.     schedule.run()  
  17.       
  18.   
  19. print("show time after 10 seconds:")  
  20. timming_exe("echo %time%"10)  
原创粉丝点击