Python normal time and unix timestamp conversion method, pythonunix
This article describes how to convert the normal time and unix timestamp of python. Share it with you for your reference. The specific analysis is as follows:
This code can be used to convert the regular time format to a unix timestamp, or convert the unix timestamp back,
For example, 1332888820 format is converted to 06:53:40 format
#-*-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
I hope this article will help you with Python programming.