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

  1. #! /usr/bin/env python
  2. #coding=utf-8
  3. # 获取今天、昨天和明天的日期
  4. # 引入datetime模块
  5. import datetime
  6. #计算今天的时间
  7. today = datetime.date.today()
  8. #计算昨天的时间
  9. yesterday = today - datetime.timedelta(days = 1)
  10. #计算明天的时间
  11. tomorrow = today + datetime.timedelta(days = 1)
  12. #打印这三个时间
  13. print(yesterday, today, tomorrow)

2.计算上一个的时间

方法1:

  1. #! /usr/bin/env python
  2. #coding=utf-8
  3. # 计算上一个的时间
  4. #引入datetime,calendar两个模块
  5. import datetime,calendar
  6.  
  7. last_friday = datetime.date.today()
  8. oneday = datetime.timedelta(days = 1)
  9.  
  10. while last_friday.weekday() != calendar.FRIDAY:
  11. last_friday -= oneday
  12.  
  13. print(last_friday.strftime('%A, %d-%b-%Y'))

方法二:借助模运算寻找上一个星期五

  1. #! /usr/bin/env python
  2. #coding=utf-8
  3. # 借助模运算,可以一次算出需要减去的天数,计算上一个星期五
  4. #同样引入datetime,calendar两个模块
  5. import datetime
  6. import calendar
  7.  
  8. today = datetime.date.today()
  9. target_day = calendar.FRIDAY
  10. this_day = today.weekday()
  11. delta_to_target = (this_day - target_day) % 7
  12. last_friday = today - datetime.timedelta(days = delta_to_target)
  13.  
  14. print(last_friday.strftime("%d-%b-%Y"))

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

  1. #! /usr/bin/env python
  2. #coding=utf-8
  3. # 获取一个列表中的所有歌曲的播放时间之和
  4. import datetime
  5.  
  6. def total_timer(times):
  7. td = datetime.timedelta(0)
  8. duration = sum([datetime.timedelta(minutes = m, seconds = s) for m, s in times], td)
  9. return duration
  10.  
  11. times1 = [(2, 36),
  12. (3, 35),
  13. (3, 45),
  14. ]
  15. times2 = [(3, 0),
  16. (5, 13),
  17. (4, 12),
  18. (1, 10),
  19. ]
  20.  
  21. assert total_timer(times1) == datetime.timedelta(0, 596)
  22. assert total_timer(times2) == datetime.timedelta(0, 815)
  23.  
  24. print("Tests passed.\n"
  25. "First test total: %s\n"
  26. "Second test total: %s" % (total_timer(times1), total_timer(times2)))

4.反复执行某个命令

  1. #! /usr/bin/env python
  2. #coding=utf-8
  3. # 以需要的时间间隔执行某个命令
  4.  
  5. import time, os
  6.  
  7. def re_exe(cmd, inc = 60):
  8. while True:
  9. os.system(cmd);
  10. time.sleep(inc)
  11.  
  12. re_exe("echo %time%", 5)

5.定时任务

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

6.利用sched实现周期调用

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

  

 

 

Python3 - 时间处理与定时任务的更多相关文章

  1. python3 时间和日期

    Python程序可以通过多种方式来处理日期和时间.日期格式之间的转换是计算机的一个共同核心.Python的时间和日历模块能够帮助我们跟踪的日期和时间. 什么是刻度? 时间间隔以秒为单位的浮点数.特别是 ...

  2. Python3.6 Schedule模块定时任务

    本文使用Python的Schedule模块.Python访问数据库的框架SQLAIchemy 实现了一个:周期性读取mysql 数据的小示例. 一,编程环境 PyCharm2016,Anaconda3 ...

  3. Ntp时间服务器与定时任务Crontab

    一 NTP时间服务器 1 局域网内的NTP同步配置 注意 所有配置操作必须是root用户 ,局域网内node21作为NTP Server,node22,node23作为NTP Client与服务器进行 ...

  4. 临时表 数据在 内存 转移时间 将160秒的创建临时表时间放入定时任务 不到1秒的求和时间 hadoop 引入Hadoop 分布式计算

    SELECT SUM(pv) as pv_t  FROM 行 112247817表类型 InnoDB自动递增值 1082428327行格式  Compact索引长度 8.60 GB (9,235,93 ...

  5. python3 时间模块 random模块之两个小练习

    话不多说,一个是算时间的,还有一个是生成验证码的 #!usr/bin/env/ python # -*- coding:utf-8 -*- # Author: XiaoFeng import time ...

  6. python3 时间处理

    1 标记当前时间 import datetime from dateutil import tz #标记当前时间为中国时间 注意(replace 只有标记的意思没有转化的意思) datetime.da ...

  7. python3时间函数

    上一篇是生成测试报告的代码,如果重复运行测试报告名称相同会不停的覆盖,之前的测试报告也会丢失,无法追溯之前的问题.那么如何解决这个问题了呢? 首先想到的是用随机函数取随机名称,一旦生成的报告较多时,无 ...

  8. Python3.x:定时任务实现方式

    Python3.x:定时任务实现方式 Python3.x下实现定时任务的方式有很多种方式. 一.循环sleep: 最简单的方式,在循环里放入要执行的任务,然后sleep一段时间再执行.缺点是,不容易控 ...

  9. 详解java定时任务

    在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现.下面LZ就其原理.实例以及Timer缺陷三个方面来解析JavaTimer定时器. ...

随机推荐

  1. [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 ...

  2. GIT GUI的使用(转)

    前段时间跟着Ruby On Rails的toturial玩了一把Git,今天再回过头来,觉得这个版本控制工具真的很不错.下面来讲一下,在windows下如何通过git gui来管理代码. 首先,要在h ...

  3. jQuery 的 live() 方法对 hover 事件的处理

    因为hover不是标准的事件,因此无法直接使用live进行处理,故使用以下方法代替,效果一样 <script type="text/javascript"> $(&qu ...

  4. 对lua继承中self.__index = self的释疑

    首先看看从lua表中查找一个键时的流程: -- 当从表t中查找键k时,lua处理如下: -- 1.t中是否有k,有则直接返回值,否则第2步 -- 2.t是否有元表, 无则返回nil, 有则第3步 -- ...

  5. 40 JavaScript Chart and Graph Libraries for Developers--reference

    reference:http://www.egrappler.com/javascript-chart-and-graph-libraries-for-developers/ BY TEAMEGRAP ...

  6. Spring MVC学习笔记 01

    applicationcontext.xml的配置 <?xml version="1.0" encoding="UTF-8" ?> <bean ...

  7. 【Shell脚本学习17】Shell case esac语句

    case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构. case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令.case语句格式如下: ...

  8. 【Shell脚本学习11】Shell注释

    以“#”开头的行就是注释,会被解释器忽略. sh里没有多行注释,只能每一行加一个#号.只能像这样: #-------------------------------------------- # 这是 ...

  9. iOS - 提示信息 - UIAlertView、UIActionSheet、UIAlertController的实际应用

    1.UIAlertView(屏幕中央弹出框)(不需要服从代理) UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" ...

  10. Java学习笔记——显示当前日期的三种方式

    一.Date类:这是一种过时的表达方式 import java.util.Date; Date date = new Date(); System.out.println((1900+date.get ...