Python uses the time module to implement a specified time trigger example, python trigger
This example describes how to use the time module to implement a specified time trigger in Python. We will share this with you for your reference. The details are as follows:
In fact, it is very easy to specify a time for the script to process an event, such as a get request ~
There are various time methods in any language, and Python is no exception.
After help (time), we can know that time has two time representation forms:
1. timestamp notation, that is, an integer or floating point represents a time interval in seconds. The base value of this time period starts from on January 1.
2. Format representation of tuples, that is, representation of a python data structure. This tuples have nine integer types. Different time meanings.
Year (four digits, e.g. 1998)
Month (1-12)
Day (1-31)
Hours (0-23)
Minutes (0-59)
Seconds (0-59)
Weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) # runtime format, 0: indicates the normal format, 1: indicates the runtime format,-1: indicates determining based on the current date and time format
time()
Ordatetime.now()
-- Returns the current timestamp in the floating point format. Parameter not accepted
clock()
-- Returns the cpu execution time of the current program. The unix system always returns the full running time, while windows uses the timestamp when this function is called for the first time as the benchmark from the second time, rather than the program start time as the benchmark. Parameters are not accepted.
sleep()
-- A delay of one time period. Integer and floating point types are accepted.
gmtime()
-- Converts the timestamp to the UTC time tuples. Accept a floating point timestamp parameter. The default value is the current timestamp.
localtime()
-- Convert the timestamp to the format of the local time tuples. Accept a floating point timestamp parameter. The default value is the current timestamp.
asctime()
-- Convert the format of time tuples to the string format. Accepts a time tuples. The default value is the return value of localtime ().
ctime()
-- Converts the timestamp to a string. Accept a timestamp. The default value is the current timestamp. Equivalent to asctime (localtime (seconds ))
mktime()
-- Converts a local time tuple to a timestamp. It is required to accept a time tuples.
strftime()
-- Converts time tuples into strings in the specified format. Format strings and time tuples. Time tuples are optional. The default value is localtime ()
strptime()
-- Resolves the time string in the specified format to the reverse process of strftime. String accepted. The time format is required.
And its type can also be reduced, and then total_seconds () can be used to convert a time difference to s. For details, refer to the subsequent code section.
Sample Code:
Import httplibimport timedef doFirst (): from datetime import datetime, timedelta curTime = datetime. now () # print curTime desTime = curTime. replace (hour = 3, minute = 0, second = 0, microsecond = 0) # print desTime delta = desTime-curTime # print delta skipSeconds = int (delta. total_seconds () # print skipSeconds if skipSeconds = 0: return True else: if skipSeconds <0: skipSeconds + = 24*60*60 print "Must sleep % d seconds" % skipSeconds return False # You can also obtain the current time difference and count it by yourself, but consider the error, def getTime (): from datetime import datetime, timedelta curTime = datetime. now () # print curTime desTime = curTime. replace (hour = 3, minute = 0, second = 0, microsecond = 0) # print desTime delta = desTime-curTime # print delta skipSeconds = int (delta. total_seconds () if skipSeconds <0: skipSeconds + = 24*60*60 print skipSeconds return skipSecondsdef gethttp (): url = "URL" conn = httplib. HTTPConnection ("IP") conn. request (method = "GET", url = url) response = conn. getresponse () res = response. read () print res # getTime () while True: if doFirst (): gethttp () time. sleep (24*59*60) time. sleep (1) s. close ()
Note:Supported time string format symbols:
Format description remarks
% A local (locale) Simplified week name
% A local full week name
% B simplified local month name
% B local full month name
% C local Date and Time Representation
% D the day of the month (01-31)
% H the hour of the day (in 24-hour format, 00-23)
% I hour (12 hour, 01-12)
% J day of the year (001-366)
% M month (01-12)
% M minutes (00-59)
% P local am or pm Operator
% S seconds (01-61)
% U the number of weeks in a year. (00-53 Sunday is the beginning of a week .) All days before the first Sunday are placed in week 0th.
% W the day of the week (0-6, 0 is Sunday)
% W and % U are basically the same. The difference is that % W starts from Monday as a week.
% X local date
% X local time
% Y remove the year of the century (00-99)
% Y full year
% Z Time Zone name (if it does not exist, it is a null character)
% 'Character
PS: This site also provides several Unix timestamp conversion and date online tools, which are very useful for your reference:
Unix timestamp Conversion Tool:
Http://tools.jb51.net/code/unixtime
Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi
Online calendar:
Http://tools.jb51.net/bianmin/wannianli
Online calendar/Calendar Conversion Tool:
Http://tools.jb51.net/bianmin/yinli2yangli