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)
________________________________________________________________________
import time
from datetime import datetime
"""将 13 位整数的毫秒时间戳转化成本地普通时间 (字符串格式)
:param timestamp: 13 位整数的毫秒时间戳 (1456402864242)
:return: 返回字符串格式 {str}'2016-02-25 20:21:04.242000'
"""
def timestamp_to_strtime(timestamp):
local_str_time = datetime.fromtimestamp(timestamp / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')
return local_str_time
"""将 13 位整数的毫秒时间戳转化成 utc 时间 (字符串格式,含毫秒)
:param timestamp: 13 位整数的毫秒时间戳 (1456402864242)
:return: 返回字符串格式 {str}'2016-02-25 12:21:04.242000'
"""
def timestamp_to_utc_strtime(timestamp):
utc_str_time = datetime.utcfromtimestamp(timestamp / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')
return utc_str_time
print(timestamp_to_strtime(1630509445862))
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
print(stampToTime(1630509445862))
本文介绍了Python如何生成13位或16位时间戳,并详细讲解了如何反向解析这些时间戳,同时拓展讨论了Python生成唯一Id的多种方法。
4234

被折叠的 条评论
为什么被折叠?



