您好,登錄后才能下訂單哦!
我就廢話不多說了,直接上代碼吧!
import datetime import time def get_float_time_stamp(): datetime_now = datetime.datetime.now() return datetime_now.timestamp() def get_time_stamp16(): # 生成16時間戳 eg:1540281250399895 -ln datetime_now = datetime.datetime.now() print(datetime_now) # 10位,時間點相當于從UNIX TIME的紀元時間開始的當年時間編號 date_stamp = str(int(time.mktime(datetime_now.timetuple()))) # 6位,微秒 data_microsecond = str("%06d"%datetime_now.microsecond) date_stamp = date_stamp+data_microsecond return int(date_stamp) def get_time_stamp13(): # 生成13時間戳 eg:1540281250399895 datetime_now = datetime.datetime.now() # 10位,時間點相當于從UNIX TIME的紀元時間開始的當年時間編號 date_stamp = str(int(time.mktime(datetime_now.timetuple()))) # 3位,微秒 data_microsecond = str("%06d"%datetime_now.microsecond)[0:3] date_stamp = date_stamp+data_microsecond return int(date_stamp) def stampToTime(stamp): datatime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(float(str(stamp)[0:10]))) datatime = datatime+'.'+str(stamp)[10:] return datatime if __name__ == '__main__': a1 = get_time_stamp16() print(a1) print(stampToTime(a1)) a2 = get_time_stamp13() print(a2) print(stampToTime(a2))
補充拓展:關于python生成唯一Id的幾種方法小結
# coding:utf-8 import random def createRandomString(len): print ('wet'.center(10,'*')) raw = "" range1 = range(58, 65) # between 0~9 and A~Z range2 = range(91, 97) # between A~Z and a~z i = 0 while i < len: seed = random.randint(48, 122) if ((seed in range1) or (seed in range2)): continue; raw += chr(seed); i += 1 # print(raw) return raw print createRandomString(20)
print ("test".center(20,"-")) #! coding:utf-8 import uuid print u"uuid1 生成基于計算機主機ID和當前時間的UUID" print uuid.uuid1() # UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') print u"\nuuid3 基于命名空間和一個字符的MD5加密的UUID" print uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') #UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') print u"\nuuid4 隨機生成一個UUID" print uuid.uuid4() #'16fd2706-8baf-433b-82eb-8c7fada847da' print u"\nuuid5 基于命名空間和一個字符的SHA-1加密的UUID" uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') #UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') print u"\n根據十六進制字符生成UUID" x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') print u"轉換成十六進制的UUID表現字符" print str(x) # '00010203-0405-0607-0809-0a0b0c0d0e0f'''
以上這篇python生成13位或16位時間戳以及反向解析時間戳的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。