本文摘自php中文网,作者爱喝马黛茶的安东尼,侵删。
在python 开发web程序时,需要调用第三方的相关接口,在调用时,需要对请求进行签名。需要用到unix时间戳。 在python里,在网上介绍的很多方法,得到的时间戳是10位。而java里默认是13位(milliseconds,毫秒级的)。

下面介绍python获得时间戳的方法:
1、10时间戳获取方法:
1 2 3 4 5 6 7 | >>> import time
>>> t = time.time()
>>> print t
1436428326.76
>>> print int(t)
1436428326
>>>
|
强制转换是直接去掉小数位。
相关推荐:《Python视频教程》
2、13位时间戳获取方法:
(1)默认情况下python的时间戳是以秒为单位输出的float
1 2 3 4 5 | >>>
>>> import time
>>> time.time()
1436428275.207596
>>>
|
通过把秒转换毫秒的方法获得13位的时间戳:
1 2 3 | import time
millis = int( round (time.time() * 1000))
print millis
|
round()是四舍五入。
(2)
1 2 3 4 5 | import time
current_milli_time = lambda: int( round (time.time() * 1000))
Then:
>>> current_milli_time()
1378761833768
|
13位时间戳转换成时间:
1 2 3 4 5 | >>> import time
>>> now = int( round (time.time()*1000))
>>> now02 = time. strftime ( '%Y-%m-%d %H:%M:%S' ,time.localtime(now/1000))
>>> now02
'2019-06-21 09:53:17'
|
以上就是python怎么生成时间戳的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python中**是什么意思
Python实现超市扫码仪计费
Python与平台有关吗
Python安装包里idle在哪
Python编写简单网络爬虫抓取视频
linux如何安装Python
Python数据类型的区别
Python实现字符串的kmp算法
Python用什么编译器
Python搭建网站的基本步骤
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python怎么生成时间戳