1. Convert the timestamp to a time format, and then return
1 #The current formatted time is taken by default 2 #If the timestamp is passed in, convert the timestamp to the formatted time and return 3 def timestamp_to_format(timestamp=None,format='%Y-%m-%d %H: %M:%S'): 4 if timestamp: 5 time_tuple = time.localtime(timestamp) 6 time.strftime(format,time_tuple) 7 else: 8 res = time.strftime(format)#The default is the current time 9 return res 10 11 print(timestamp_to_format())
2. Convert the formatted time to a timestamp
1 def strToTimestamp(str=None,format= ' %Y%m%d %H:%M:%S ' ): 2 if str: #If the time is passed 3 tp = time.strptime(str,format ) # Convert the formatted time into a time tuple 4 res = time.mktime(tp) #Convert to timestamp 5 else : 6 res = time.time() #The default is to take the current timestamp 7 return int(res) 8 9 print (strToTimestamp()) 10 print (strToTimestamp( ' 20180501 22:22:12 ' ))