Python3 - 时间处理与定时任务

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

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


1

2

3

4

5

6

7

8

9

10

11

12

13

#! /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

2

3

4

5

6

7

8

9

10

11

12

13

#! /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‘))

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

#! /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.计算歌曲的总播放时间


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

#! /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 = [(236), 

          (335), 

          (345), 

          

times2 = [(30), 

          (513), 

          (412), 

          (110), 

          

   

assert total_timer(times1) == datetime.timedelta(0596

assert total_timer(times2) == datetime.timedelta(0815

   

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

#! /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.定时任务


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#! /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实现周期调用


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#! /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)

时间: 2024-08-12 04:37:31

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

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

话不多说,一个是算时间的,还有一个是生成验证码的 1 #!usr/bin/env/ python 2 # -*- coding:utf-8 -*- 3 # Author: XiaoFeng 4 import time 5 start_time = "2016-9-1 7:00:00" 6 handel = time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S")) 7 now = time.time() 8

具体解释java定时任务

在我们编程过程中假设须要运行一些简单的定时任务,无须做复杂的控制.我们能够考虑使用JDK中的Timer定时任务来实现. 以下LZ就其原理.实例以及Timer缺陷三个方面来解析java Timer定时器. 一.简单介绍 在java中一个完整定时任务须要由Timer.TimerTask两个类来配合完毕. API中是这样定义他们的.Timer:一种工具.线程用其安排以后在后台线程中运行的任务.可安排任务运行一次,或者定期反复运行.由TimerTask:Timer 安排为一次运行或反复运行的任务.我们能

linux 定时任务 crond 服务应用指南

linux定时任务crond服务应用指南 Linux的定时任务crond(crontab)服务 1.crond是什么? crond是linux系统中用来定期执行命令或者指定程序的程序(软件) 2.秒级任务 (1)crond需要方式实现 (2)自己写守护进程shell循环 (3)Quartz也可实现妙计任务 3.定时任务的作用 服务器:7*24小时开机提供服务是网站的基本特征 有重要数据:提高备份周期和备份数量 Linux的定时任务分类 linux系统中定时任务调度的工作可以分为一下两种 (1)系

Java 定时任务

在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现.下面LZ就其原理.实例以及Timer缺陷三个方面来解析JavaTimer定时器. 一.简介 在Java中一个完整定时任务需要由Timer.TimerTask两个类来配合完成. API中是这样定义他们的,Timer:一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行.由TimerTask:Timer 安排为一次执行或重复执行的任务.我们可以这样理

linux应用之crontab定时任务的设置

实现Linux定时任务有:cron.anacron.at等,这里主要介绍cron服务. 名词解释: cron是服务名称,crond是后台进程,crontab则是定制好的计划任务表. 软件包安装: 要使用cron服务,先要安装vixie-cron软件包和crontabs软件包,两个软件包作用如下: vixie-cron软件包是cron的主程序.crontabs软件包是用来安装.卸装.或列举用来驱动 cron 守护进程的表格的程序. 查看是否安装了cron软件包: rpm -qa|grep vixi

spring 动态定时任务

功能介绍:商品自动上架.按修改或添加时设置的自动上架时间而启动定时任务 更改商品状态为上架. spring 中配置文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&q

spring squertz定时任务(二)

spring squertz是一个强大的定时任务处理方式 1.需要的Jar quartz-1.8.5.jar commons-logging.jar spring-core-3.0.5.RELEASE.jar spring-beans-3.0.5.RELEASE.jar spring-context-3.0.5.RELEASE.jar spring-context-support-3.0.5.RELEASE.jar spring-asm-3.0.5.RELEASE.jar spring-expr

java定时任务

一.简介 在java编程中,有时需要在指定时间或者指定频率来执行任务,可以使用Timer和TimerTask定时器. Timer类 在工具类Timer中,提供了四个构造方法,每个构造方法都启动了计时器线程,同时Timer类可以保证多个线程可以共享单个Timer对象而无需进行外部同步,所以Timer类是线程安全的.但是由于每一个Timer对象对应的是单个后台线程,用于顺序执行所有的计时器任务,一般情况下我们的线程任务执行所消耗的时间应该非常短,但是由于特殊情况导致某个定时器任务执行的时间太长,那么

linux定时任务cron配置[转]

实现linux定时任务有:cron.anacron.at等,这里主要介绍cron服务. 名词解释: cron是服务名称,crond是后台进程,crontab则是定制好的计划任务表. 软件包安装: 要使用cron服务,先要安装vixie-cron软件包和crontabs软件包,两个软件包作用如下: vixie-cron软件包是cron的主程序.crontabs软件包是用来安装.卸装.或列举用来驱动 cron 守护进程的表格的程序. 查看是否安装了cron软件包: rpm -qa|grep vixi