In Python, there are usually several ways to represent time:
Timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type.
Formatted time string (format string)
Structured time (Struct_time): Struct_time A total of 9 elements in total nine elements: (year, month, day, hour, minute, second, week of the year, Day of the year, summer Time)
Import time#--------------------------We start with the current time, so that we can quickly understand three forms of time print (Time.time ()) # Timestamp: 1487130156.419527print ( Time.strftime ("%y-%m-%d%x") #格式化的时间字符串: ' 2017-02-15 11:40:53 ' Print (Time.localtime ()) #本地时区的struct_timeprint ( Time.gmtime ()) #UTC时区的struct_time
The conversions between the various time forms are as follows:
#------------------------ --as shown in Figure 1 conversion time # localtime ([secs]) # Converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time. Time.localtime () Time.localtime (1473525444.037215) # gmtime ([secs]) and localtime () method similar to Gmtime () The Struct_time method is to convert a timestamp to the UTC time zone (0 o'clock Zone). # mktime (t): Converts a struct_time to a timestamp. Print (Time.mktime (Time.localtime ())) #1473525749.0# strftime (format[, T]): A tuple representing time or struct_ Time, such as returned by Time.localtime () and # Time.gmtime (), is converted to a formatted date string. If T is not specified, the Time.localtime () is passed in. If any of the # elements in the tuple are out of bounds, the ValueError error will be thrown. Print (Time.strftime ("%y-%m-%d%x", Time.localtime ())) #2016 -09-11 00:49:56# time.strptime (string[, format]) # Converts a formatted time string to Struct_time. In fact it is inverse operation with strftime (). Print (Time.strptime (' 2011-05-05 16:37:06 ', '%y-%m-%d%x ')) #time. Struct_time (tm_year=2011, tm_mon=5, tm_mday=5, tm_ Hour=16, tm_min=37, tm_sec=6,# tm_wday=3, tm_yday=125, tm_isdst=-1) #在这个函数中, format defaults to: "%a%b%d%h:%m:%s%Y".
#--------------------------2 Conversion Time # asctime ([t]): A tuple or struct_time representing time is represented in this form: ' Sun June 20 23:21:05 1993 '. # If there are no arguments, time.localtime () will be passed in as a parameter. Print (Time.asctime ()) #Sun Sep one 00:43:43 2016# ctime ([secs]): Converts a timestamp (floating point number in seconds) to the form of Time.asctime (). If the parameter is not given or is # None, the default Time.time () is the parameter. Its function is equivalent to Time.asctime (time.localtime (secs)). Print (Time.ctime ()) # Sun Sep 00:46:38 2016print (time.ctime (Time.time ())) # Sun Sep 11 00:46:38 2016
#时间加减import datetime# Print (Datetime.datetime.now ()) #返回 2016-08-19 12:47:03.941925#print ( Datetime.date.fromtimestamp (Time.time ())) # Timestamp goes directly to date format 2016-08-19# print (Datetime.datetime.now ()) # Print ( Datetime.datetime.now () + Datetime.timedelta (3)) #当前时间 + 3 days # print (Datetime.datetime.now () + Datetime.timedelta (-3)) # Current Time-3 days # print (Datetime.datetime.now () + Datetime.timedelta (hours=3)) #当前时间 + 3 hours # print (Datetime.datetime.now () + Datetime.timedelta (minutes=30)) #当前时间 +30 points # # # C_time = Datetime.datetime.now () # Print (C_time.replace (minute=3, hour=2)) #时间替换datetime模块
How to defer the use of threads:
# Sleep (secs) # thread postpones the specified time to run, in seconds.
Python Time module