Python3 - 时间处理与定时任务
1.计算明天和昨天的日期
- #! /usr/bin/env python
- #coding=utf-8
- # 获取今天、昨天和明天的日期
- # 引入datetime模块
- import datetime
- #计算今天的时间
- today = datetime.date.today()
- #计算昨天的时间
- yesterday = today - datetime.timedelta(days = 1)
- #计算明天的时间
- tomorrow = today + datetime.timedelta(days = 1)
- #打印这三个时间
- print(yesterday, today, tomorrow)
2.计算上一个的时间
方法1:
- #! /usr/bin/env python
- #coding=utf-8
- # 计算上一个的时间
- #引入datetime,calendar两个模块
- 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'))
方法二:借助模运算寻找上一个星期五
- #! /usr/bin/env python
- #coding=utf-8
- # 借助模运算,可以一次算出需要减去的天数,计算上一个星期五
- #同样引入datetime,calendar两个模块
- 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.计算歌曲的总播放时间
- #! /usr/bin/env python
- #coding=utf-8
- # 获取一个列表中的所有歌曲的播放时间之和
- 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.反复执行某个命令
- #! /usr/bin/env python
- #coding=utf-8
- # 以需要的时间间隔执行某个命令
- import time, os
- def re_exe(cmd, inc = 60):
- while True:
- os.system(cmd);
- time.sleep(inc)
- re_exe("echo %time%", 5)
5.定时任务
- #! /usr/bin/env python
- #coding=utf-8
- #这里需要引入三个模块
- 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):
- # enter用来安排某事件的发生时间,从现在起第n秒开始启动
- schedule.enter(inc, 0, perform_command, (cmd, inc))
- # 持续运行,直到计划时间队列变成空为止
- schedule.run()
- print("show time after 10 seconds:")
- timming_exe("echo %time%", 10)
6.利用sched实现周期调用
- #! /usr/bin/env python
- #coding=utf-8
- import time, os, sched
- # 第一个参数确定任务的时间,返回从某个特定的时间到现在经历的秒数
- # 第二个参数以某种人为的方式衡量时间
- schedule = sched.scheduler(time.time, time.sleep)
- def perform_command(cmd, inc):
- # 安排inc秒后再次运行自己,即周期运行
- schedule.enter(inc, 0, perform_command, (cmd, inc))
- os.system(cmd)
- def timming_exe(cmd, inc = 60):
- # enter用来安排某事件的发生时间,从现在起第n秒开始启动
- schedule.enter(inc, 0, perform_command, (cmd, inc))
- # 持续运行,直到计划时间队列变成空为止
- schedule.run()
- print("show time after 10 seconds:")
- timming_exe("echo %time%", 10)
Python3 - 时间处理与定时任务的更多相关文章
- python3 时间和日期
Python程序可以通过多种方式来处理日期和时间.日期格式之间的转换是计算机的一个共同核心.Python的时间和日历模块能够帮助我们跟踪的日期和时间. 什么是刻度? 时间间隔以秒为单位的浮点数.特别是 ...
- Python3.6 Schedule模块定时任务
本文使用Python的Schedule模块.Python访问数据库的框架SQLAIchemy 实现了一个:周期性读取mysql 数据的小示例. 一,编程环境 PyCharm2016,Anaconda3 ...
- Ntp时间服务器与定时任务Crontab
一 NTP时间服务器 1 局域网内的NTP同步配置 注意 所有配置操作必须是root用户 ,局域网内node21作为NTP Server,node22,node23作为NTP Client与服务器进行 ...
- 临时表 数据在 内存 转移时间 将160秒的创建临时表时间放入定时任务 不到1秒的求和时间 hadoop 引入Hadoop 分布式计算
SELECT SUM(pv) as pv_t FROM 行 112247817表类型 InnoDB自动递增值 1082428327行格式 Compact索引长度 8.60 GB (9,235,93 ...
- python3 时间模块 random模块之两个小练习
话不多说,一个是算时间的,还有一个是生成验证码的 #!usr/bin/env/ python # -*- coding:utf-8 -*- # Author: XiaoFeng import time ...
- python3 时间处理
1 标记当前时间 import datetime from dateutil import tz #标记当前时间为中国时间 注意(replace 只有标记的意思没有转化的意思) datetime.da ...
- python3时间函数
上一篇是生成测试报告的代码,如果重复运行测试报告名称相同会不停的覆盖,之前的测试报告也会丢失,无法追溯之前的问题.那么如何解决这个问题了呢? 首先想到的是用随机函数取随机名称,一旦生成的报告较多时,无 ...
- Python3.x:定时任务实现方式
Python3.x:定时任务实现方式 Python3.x下实现定时任务的方式有很多种方式. 一.循环sleep: 最简单的方式,在循环里放入要执行的任务,然后sleep一段时间再执行.缺点是,不容易控 ...
- 详解java定时任务
在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现.下面LZ就其原理.实例以及Timer缺陷三个方面来解析JavaTimer定时器. ...
随机推荐
- [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
Each time you use the Async Pipe, you create a new subscription to the stream in the template. This ...
- GIT GUI的使用(转)
前段时间跟着Ruby On Rails的toturial玩了一把Git,今天再回过头来,觉得这个版本控制工具真的很不错.下面来讲一下,在windows下如何通过git gui来管理代码. 首先,要在h ...
- jQuery 的 live() 方法对 hover 事件的处理
因为hover不是标准的事件,因此无法直接使用live进行处理,故使用以下方法代替,效果一样 <script type="text/javascript"> $(&qu ...
- 对lua继承中self.__index = self的释疑
首先看看从lua表中查找一个键时的流程: -- 当从表t中查找键k时,lua处理如下: -- 1.t中是否有k,有则直接返回值,否则第2步 -- 2.t是否有元表, 无则返回nil, 有则第3步 -- ...
- 40 JavaScript Chart and Graph Libraries for Developers--reference
reference:http://www.egrappler.com/javascript-chart-and-graph-libraries-for-developers/ BY TEAMEGRAP ...
- Spring MVC学习笔记 01
applicationcontext.xml的配置 <?xml version="1.0" encoding="UTF-8" ?> <bean ...
- 【Shell脚本学习17】Shell case esac语句
case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构. case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: ...
- 【Shell脚本学习11】Shell注释
以“#”开头的行就是注释,会被解释器忽略. sh里没有多行注释,只能每一行加一个#号.只能像这样: #-------------------------------------------- # 这是 ...
- iOS - 提示信息 - UIAlertView、UIActionSheet、UIAlertController的实际应用
1.UIAlertView(屏幕中央弹出框)(不需要服从代理) UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" ...
- Java学习笔记——显示当前日期的三种方式
一.Date类:这是一种过时的表达方式 import java.util.Date; Date date = new Date(); System.out.println((1900+date.get ...