basic knowledge
- The processing of time can be classified into three types
Time display
Time conversion
Time calculations
- Several commonly used time representation methods in python
Timestamp: The total number of seconds starting from 0:00 on January 1, 1970
Format time string according to custom format: for example "2023-11-7 11:41"
Tuple
- UTC: Coordinated Universal Time, universal standard time has no time zone, and the Chinese background is the East Eighth District, which is UTC+8 hours
time module: generally used to print timestamps and convert time formats
time.localtime([secs]) #Convert timestamp to tuple, default is current timestamp. Parameters can be modified to other timestamps
time.gmtime([secs]) #Similar to time.localtime(), but it displays UTC time minus 8 hours in China
time.time() #Timestamp
time.mktime() #Convert tuples to timestamps
time.sleep() #How many seconds to sleep
time.sleep() #Convert the tuple to the format of "Tue Nov 7 12:07:52 2023", which defaults to the current timestamp
time.ctime() #Convert the timestamp to the format of "Tue Nov 7 12:07:52 2023". The default is the current timestamp.
time.strftime(format[,t]) #Format the tuple into a string, the default is the current time
Example:
time.strftime("%Y-%m-%d %X")
time.strftime("%Y-%m-%d %H:%M")
print(time.strptime(str[,format)]) #Convert string to tuple
Example:
time.strptime("2023-11-07 12:16","%Y-%m-%d %H:%M")
datetime module: generally used to display dates and date operations
datetime.date class representing date
datetime.date.today() #Date
datetime.datetime #Represents the date and time class
datetime.datetime.now() #Current time
datetime.timedelta #Time interval operation class
Example:
t = datetime.datetime.now()
T -dates.timedelta (Days = 3) #At present, minus 3 days
t + datetime.timedelta(days=-3,minutes=30) #Current time minus 3 days plus 30 minutes
t.replace(year=2015) #Replace the current time and year with 2015
pytz time zone module