Python3中常用的处理时间和实现定时任务的方法的介绍
《时间清单法》- 制定并跟踪任务清单的实用方法 #生活常识# #时间管理建议# #时间管理书籍#
2020-05-31 10:44脚本之家 Python
这篇文章主要介绍了Python3中常用的处理时间和实现定时任务的方法,包括循环执行某个任务这样的实现,需要的朋友可以参考下
无论哪种编程语言,时间肯定都是非常重要的部分,今天来看一下python如何来处理时间和python定时任务,注意咯:本篇所讲是python3版本的实现,在python2版本中的实现略有不同,有时间会再写一篇以便大家区分。
1.计算明天和昨天的日期
?
1
2
3
4
5
6
7
8
9
10
11
12
13
import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
tomorrow = today + datetime.timedelta(days = 1)
print(yesterday, today, tomorrow)
2.计算上一个的时间
方法一:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
import datetime,calendar
last_friday = datetime.date.today()
oneday = datetime.timedelta(days = 1)
while last_friday.weekday() != calendar.FRIDAY:
last_friday -= oneday
print(last_friday.strftime('%A, %d-%b-%Y'))
方法二:借助模运算寻找上一个星期五
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import datetime
import calendar
today = datetime.date.today()
target_day = calendar.FRIDAY
this_day = today.weekday()
delta_to_target = (this_day - target_day) % 7
last_friday = today - datetime.timedelta(days = delta_to_target)
print(last_friday.strftime("%d-%b-%Y"))
3.计算歌曲的总播放时间
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import datetime
def total_timer(times):
td = datetime.timedelta(0)
duration = sum([datetime.timedelta(minutes = m, seconds = s) for m, s in times], td)
return duration
times1 = [(2, 36),
(3, 35),
(3, 45),
]
times2 = [(3, 0),
(5, 13),
(4, 12),
(1, 10),
]
assert total_timer(times1) == datetime.timedelta(0, 596)
assert total_timer(times2) == datetime.timedelta(0, 815)
print("Tests passed.\n"
"First test total: %s\n"
"Second test total: %s" % (total_timer(times1), total_timer(times2)))
4.反复执行某个命令
?
1
2
3
4
5
6
7
8
9
10
11
12
import time, os
def re_exe(cmd, inc = 60):
while True:
os.system(cmd);
time.sleep(inc)
re_exe("echo %time%", 5)
5.定时任务
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import time, os, sched
schedule = sched.scheduler(time.time, time.sleep)
def perform_command(cmd, inc):
os.system(cmd)
def timming_exe(cmd, inc = 60):
schedule.enter(inc, 0, perform_command, (cmd, inc))
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)
6.利用sched实现周期调用
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import time, os, sched
schedule = sched.scheduler(time.time, time.sleep)
def perform_command(cmd, inc):
schedule.enter(inc, 0, perform_command, (cmd, inc))
os.system(cmd)
def timming_exe(cmd, inc = 60):
schedule.enter(inc, 0, perform_command, (cmd, inc))
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)


python实现k-means聚类算法
这篇文章主要为大家详细介绍了python实现k-means聚类算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...
7422021-01-16



使用pygame模块编写贪吃蛇的实例讲解
下面小编就为大家分享一篇使用pygame模块编写贪吃蛇的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...
10902021-01-12

Python装饰器模式定义与用法分析
这篇文章主要介绍了Python装饰器模式定义与用法,结合实例形式分析了Python装饰器模式的具体定义、使用方法及相关操作技巧,需要的朋友可以参考下...
6362021-03-26


网址:Python3中常用的处理时间和实现定时任务的方法的介绍 https://www.yuejiaxmz.com/news/view/855755
相关内容
spring的@Scheduled定时任务介绍优化时间利用和任务处理的实用技巧.docx
Python怎么实现定时任务?python自动化定时方法
python3读取文件和异常处理(七)
linux中的延时定时任务管理
生活中处理时间的减压方法
Python定时任务,三步实现自动化
时间管理的常用方法
十个实用的时间管理方法
Obsidian:实现任务+时间管理【Day Planner】(简易版)