Regardless of which programming language, time is certainly a very important part, today to see how Python handles time and Python timing tasks, note: This article is the Python3 version of the implementation, in the Python2 version of the implementation of a slightly different, there is time to write another for everyone to distinguish.
1. Calculate Tomorrow and yesterday's date
#! /usr/bin/env python#coding=utf-8# get today, yesterday and tomorrow's date # introduce a datetime module import datetime #计算今天的时间today = Datetime.date.today () # Calculate yesterday's Time yesterday = Today-datetime.timedelta (days = 1) #计算明天的时间tomorrow = today + datetime.timedelta (day = 1) #打印这三个时间pr Int (Yesterday, today, tomorrow)
2. Calculate the last time
Method One:
#! /usr/bin/env python#coding=utf-8# calculates the last time # introduced Datetime,calendar two modules 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 '))
Method two: Using modulo arithmetic to find the last Friday
#! /usr/bin/env python#coding=utf-8# with modulo operations, you can calculate the number of days to subtract at a time, and the previous Friday # also introduces Datetime,calendar two modules import datetime import Calendar today = Datetime.date.today () target_day = Calendar. FRIDAY this_day = Today.weekday () Delta_to_target = (this_day-target_day)% 7last_friday = Today-datetime.timedelta (da ys = delta_to_target) print (Last_friday.strftime ("%d-%b-%y"))
3. Calculate the total playback time of the song
#! /usr/bin/env python#coding=utf-8# Get a list of all the songs in the Play Time and 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, 3), ( 3, 3), ] Times2 = [(0, 5) , (+ +), (4,), (1, 10), c11/>] 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), t Otal_timer (Times2)))
4. Repeatedly execute a command
#! /usr/bin/env python#coding=utf-8# executes a command at the required interval import time, OS def re_exe (cmd, inc =): while true:< C20/>os.system (cmd); Time.sleep (inc) Re_exe ("Echo%time%", 5)
5. Scheduled Tasks
#! /usr/bin/env python#coding=utf-8# here need to introduce three modules import time, OS, Sched # The first parameter determines when a task is returned, returning the number of seconds from a specific time to the present experience # The second parameter measures time in some artificial way schedule = Sched.scheduler (Time.time, Time.sleep) def perform_command (CMD, Inc): Os.system (CMD) def timming_exe (cmd, inc = $): # Enter is used to schedule the occurrence of an event, starting from now on in nth Seconds Schedule.enter (inc, 0, Perform_command, (CMD, INC) # continues to run until the scheduled time queue becomes empty until schedule.run () print ("Show hours after Seconds:") Timming_exe ("Echo%time%", 10)
6. Using Sched to implement periodic calls
#! /usr/bin/env python#coding=utf-8import time, OS, Sched # The first parameter determines the duration of a task, and returns the number of seconds from a specific time to the present # The second parameter measures time in some artificial way Schedule = Sched.scheduler (Time.time, Time.sleep) def perform_command (CMD, Inc): # Schedule an inc second to run itself again, i.e. cycle run Schedule.enter (inc, 0, Perform_command, (CMD, Inc)) Os.system (cmd) def timming_exe (cmd, inc =): # Enter is used to schedule the occurrence of an event, starting from now n seconds start Schedule.enter (inc, 0, Perform_command, (CMD, Inc)) # Continue running until the scheduled time queue becomes empty Schedule.run () print ("Show Time After Seconds:") Timming_exe ("Echo%time%", 10)