Time Module
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
- Tuple (Struct_time): There are nine elements in a total of 9 elements: (year, month, day, time, minute, second, week of the year, Day of the Year, summer)
1 Import Time2 3 #1 time (): Returns the timestamp of the current time4Time.time ()#1473835903.02782635 6 #----------------------------------------------------------7 8 #2 localtime ([secs])9 #converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time. TenTime.localtime ()#time.struct_time (tm_year=2016, tm_mon=9, tm_mday=14, tm_hour=14, tm_min=53, tm_sec=18, tm_wday=2, tm_yday=258, tm_ isdst=0) OneTime.localtime (1473835903.0278263) A - #---------------------------------------------------------- - the #3 Gmtime ([secs]) and localtime () methods are similar, the Gmtime () method is the struct_time of converting a timestamp to the UTC time zone (0 o'clock Zone). - - #---------------------------------------------------------- - + #4 mktime (t): Converts a struct_time to a timestamp. - Print(Time.mktime (Time.localtime ()))#1473836098.0 + A #---------------------------------------------------------- at - #5 asctime ([t]): A tuple or struct_time representing time is expressed in this form: ' Sun June 20 23:21:05 1993 '. - #If there are no parameters, Time.localtime () will be passed in as a parameter. - Print(Time.asctime ())#Wed Sep 14:55:39 - - #---------------------------------------------------------- in - #6 CTime ([secs]): Converts a timestamp (floating point number in seconds) to the form of Time.asctime (). If the parameter is not given or is to #None, the default Time.time () will be the parameter. Its function is equivalent to Time.asctime (time.localtime (secs)). + Print(Time.ctime ())#Wed Sep 14:55:39 - the Print(Time.ctime (Time.time ()))#Wed Sep 14:55:39 * $ #7 strftime (format[, T]): Put a tuple or struct_time that represents time (such as by Time.localtime () andPanax Notoginseng #Time.gmtime () returns) into the formatted time string. If T is not specified, the Time.localtime () is passed in. If any one of the tuples - #elements are out of bounds, valueerror errors will be thrown. the Print(Time.strftime ("%y-%m-%d%x", Time.localtime ()))#2016-09-14 14:58:12 + A #8 Time.strptime (string[, format]) the #converts a formatted time string to Struct_time. In fact it is inverse operation with strftime (). + Print(Time.strptime ('2016-09-14 14:58:12','%y-%m-%d%x')) - $ #time.struct_time (tm_year=2016, tm_mon=9, tm_mday=14, tm_hour=14, tm_min=58, tm_sec=12, tm_wday=2, tm_yday=258, tm_ Isdst=-1) $ - #In this function, format defaults to: "%a%b%d%h:%m:%s%Y". - the - #9 Sleep (secs)Wuyi #The thread defers the specified time run, in seconds. the - #ten clock () Wu #It is important to note that the meanings are different on different systems. On a UNIX system, it returns "process time", which is a floating-point number (timestamp) in seconds. - #in Windows, the first call returns the actual time that the process is running. After the second call is run from the first call to the present About #time, that is, two time difference. Time Module
Python common modules