python生成13位或16位时间戳以及反向解析时间戳的实例
作者:宁致乐水 时间:2021-02-18 17:16:35
我就废话不多说了,直接上代码吧!
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'''
来源:https://blog.csdn.net/qq_31603575/article/details/83343791
标签:python,13位,16位,时间戳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
sql server not in 语句使程充崩溃
2012-01-05 19:05:00
Python深度学习之Pytorch初步使用
2023-08-12 08:16:41
![](https://img.aspxhome.com/file/2023/5/68895_0s.png)
[译]2009年海外Web设计风潮(下)
2009-01-23 09:34:00
![](https://img.aspxhome.com/file/UploadPic/20091/23/trend-38-39s.gif)
基于Python中单例模式的几种实现方式及优化详解
2022-10-24 14:20:45
针对SQL Server中业务规则链接的分析
2009-01-20 11:43:00
显示日期格式的几个函数
2008-09-03 13:20:00
php bugs代码审计基础详解
2023-06-02 13:49:52
![](https://img.aspxhome.com/file/2023/6/55366_0s.png)
sqlserver 数据库学习笔记
2011-12-01 08:15:06
onerror图片失效时显示其他图片
2008-08-11 21:05:00
position:relative/absolute无法冲破的等级
2007-05-11 17:03:00
Python使用 Beanstalkd 做异步任务处理的方法
2021-01-06 18:18:19
![](https://img.aspxhome.com/file/2023/1/75151_0s.png)
发现几处IE与firefox的js和css几处不同点
2008-02-02 10:15:00
python计算方程式根的方法
2023-08-09 09:54:47
MYSQL 字符串操作
2008-11-13 12:34:00
常用ASCII 码对照表
2007-08-21 14:35:00
Django-migrate报错问题解决方案
2021-11-05 13:30:16
php 读取文件头判断文件类型的实现代码
2023-11-15 09:50:06
Python 中的lambda匿名函数和三元运算符
2023-04-21 05:22:18
WEB2.0网页制作标准教程(11)不用表格的菜单
2008-02-19 19:36:00
PHP查询快递信息的方法
2023-11-14 10:25:48