程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The time module can be used in python3 to realize time processing and timing tasks

編輯:Python

1. Calculate the date of tomorrow and yesterday

Get today 、 The dates of yesterday and tomorrow

# introduce datetime modular 
import datetime
# Calculate today's time 
today = datetime.date.today()
# Calculate yesterday's time 
yesterday = today - datetime.timedelta(days = 1)
# Calculate tomorrow's time 
tomorrow = today + datetime.timedelta(days = 1)
# Print these three times 
print(yesterday, today, tomorrow)

2. Calculate the last time

Method 1 :

Calculate the last time
introduce 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 2 : Find the last Friday with the help of modular operation

With the help of modular operation , You can calculate the number of days to subtract at a time , Calculate the last Friday
Also introduce 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) % 7
last_friday = today - datetime.timedelta(days = delta_to_target)
print(last_friday.strftime("%d-%b-%Y"))

3. Calculate the total playback time of the song

Get the sum of playing time of all songs in a list

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. Execute a command repeatedly

Execute a command at the required interval

import time, os
def re_exe(cmd, inc = 60):
while True:
os.system(cmd);
time.sleep(inc)
re_exe("echo %time%", 5)

5. Timing task

Three modules need to be introduced here

''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
import time, os, sched
# The first parameter determines the time of the task , Returns the number of seconds elapsed 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):
os.system(cmd)
def timming_exe(cmd, inc = 60):
# enter Used to schedule an event , From now on n Start in seconds 
schedule.enter(inc, 0, perform_command, (cmd, inc))
# Keep running , Until the scheduled time queue becomes empty 
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)

6. utilize sched Implement periodic call

import time, os, sched
# The first parameter determines the time of the task , Returns the number of seconds elapsed 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):
# arrange inc Seconds later, run yourself again , I.e. periodic operation 
schedule.enter(inc, 0, perform_command, (cmd, inc))
os.system(cmd)
def timming_exe(cmd, inc = 60):
# enter Used to schedule an event , From now on n Start in seconds 
schedule.enter(inc, 0, perform_command, (cmd, inc))
# Keep running , Until the scheduled time queue becomes empty 
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved