Time Comparison of string formats in python, python strings
Python has multiple time-related modules, namely time, datetime, and calendar. Today we will focus on time writing.
The time module mainly includes the following methods:
Ltime = time. time () Get the current system time and return the float type value timestamp (the offset of the current time relative to 00:00:00 in seconds );
Localtime = time. localtime (ltime) converts a float timestampCurrent Time ZoneStruct time (actually an array );
Localtime = time. gmtime (ltime) converts a float timestampUTC time zoneStruct time (actually an array );
Ltime = time. mktime (localtime) converts str uct time to a float timestamp.
Strtime = time. strftime ('% Y % m % d % H % M % s', localtime). The localtime parameter is the struct time and returns a string.
Localtime = time. strptime (strtime, '% Y % m % d % H % M % s'), exactly the reverse operation of strftime, The strtime parameter string format date.
For example, comparison after a time offset:
1 #-*-coding = UTF-8-*-2 _ author __= 'zhongtang '3 4 ''' 5 Timestamp and String Conversion 6 ''' 7 8 import time 9 10 localtime1 = time. localtime () 11 time. sleep (5) 12 localtime2 = time. localtime (time. time () 13 14 print type (localtime1), localtime115 print type (localtime2), localtime216 17 gmtime = time. gmtime (time. time () 18 print type (gmtime), gmtime19 20 21 strtime1 = '000000' 22 strtime2 = '000000' 23 24 # string into time data structure 25 localtime1 = time. strptime (strtime1, '% Y % m % d % H % M % s') 26 localtime2 = time. strptime (strtime2, '% Y % m % d % H % M % s') 27 28 print type (localtime1), localtime129 print type (localtime2 ), localtime230 31 32 # convert from time data structure to timestamp 33 time1 = time. mktime (localtime1) 34 time2 = time. mktime (localtime2) 35 36 print type (time1), timestamps print type (time2), time238 39 # The timestamp can be directly subtracted to obtain the difference of 40 print time2-time1 in seconds
Output result
1 <type 'time.struct_time'> time.struct_time(tm_year=2016, tm_mon=5, tm_mday=19, tm_hour=9, tm_min=9, tm_sec=30, tm_wday=3, tm_yday=140, tm_isdst=0)2 <type 'time.struct_time'> time.struct_time(tm_year=2016, tm_mon=5, tm_mday=19, tm_hour=9, tm_min=9, tm_sec=35, tm_wday=3, tm_yday=140, tm_isdst=0)3 <type 'time.struct_time'> time.struct_time(tm_year=2016, tm_mon=5, tm_mday=19, tm_hour=1, tm_min=9, tm_sec=35, tm_wday=3, tm_yday=140, tm_isdst=0)4 <type 'time.struct_time'> time.struct_time(tm_year=2016, tm_mon=5, tm_mday=18, tm_hour=1, tm_min=1, tm_sec=1, tm_wday=2, tm_yday=139, tm_isdst=-1)5 <type 'time.struct_time'> time.struct_time(tm_year=2016, tm_mon=5, tm_mday=18, tm_hour=2, tm_min=1, tm_sec=1, tm_wday=2, tm_yday=139, tm_isdst=-1)6 <type 'float'> 1463504461.07 <type 'float'> 1463508061.08 3600.0