The examples in this article describe the ways in which Python's normal time and Unix timestamp are converted to each other. Share to everyone for your reference. The specific analysis is as follows:
This code can be used to convert a regular time format to a Unix timestamp, or to convert a Unix timestamp back,
For example: 1332888820 format converted to 2012-03-28 06:53:40 form
#-*-Coding:utf-8-*-import Time def timestamp_datetime (value): Format = '%y-%m-%d%H: %m:%s ' # value is the passed-in value for the timestamp (shaping), such as: 1332888820 value = Time.localtime (value) # # after localtime converted to # # time.struct_time (tm_year =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 go through the strftime function into normal day
Period format. DT = time.strftime (format, value) return dt def datetime_timestamp (DT): #dt为字符串 #中间过程, it is generally necessary to convert a string into a time array time.strptim E (DT, '%y-%m-%d%h:%m:%s ') # # Time.struct_time (tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, Tm_w
day=2, tm_yday=88, tm_isdst=-1) #将 "2012-03-28 06:53:40" converted to timestamp s = time.mktime (time.strptime (DT, '%y-%m-%d%h:%m:%s ')) return int (s) if __name__ = = ' __main__ ': D = datetime_timestamp (' 2012-03-28 06:53:40 ') print d s = timestamp_datetime (1332888820) print S
PS: Here again for you to recommend a local UNIX timestamp conversion tool, with a variety of languages (python/php/java/mysql, etc.) Unix timestamp operation methods:
Unix Timestamp (timestamp) conversion tool:Http://tools.jb51.net/code/unixtime
I hope this article will help you with your Python programming.