UNIX timestamp conversion (Python)
#-*-Coding: UTF-8-*-import timedef timestamp_datetime (value): format = '% Y-% m-% d % H: % m: % s' # value indicates that the input value is a timestamp (integer), for example: 1332888820 value = time. localtime (value) # converted to # Time after localtime conversion. struct_time (maid = 2012, tm_mon = 3, tm_mday = 28, tm_hour = 6, tm_min = 53, tm_sec = 40, tm_wday = 2, tm_yday = 88, tm_isdst = 0) # Finally, convert the strftime function to the normal date format. Dt = time. strftime (format, value) return dtdef datetime_timestamp (DT): # dt is a string # intermediate process, which usually needs to be converted into a time array. strptime (DT, '% Y-% m-% d % H: % m: % s') # Time. struct_time (maid = 2012, tm_mon = 3, tm_mday = 28, tm_hour = 6, tm_min = 53, tm_sec = 40, tm_wday = 2, tm_yday = 88, tm_isdst =-1) # convert "06:53:40" to the timestamp S = time. mktime (time. strptime (DT, '% Y-% m-% d % H: % m: % s') return int (s) if _ name _ = '_ main _': D = datetime_timestamp ('2017-03-28 06:53:40 ') print d s = timestamp_datetime (2012) print s