最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python获取时间戳如何实现 python获取时间戳实现代码
时间:2020-09-23 编辑:袖梨 来源:一聚教程网
python获取时间戳如何实现?本篇文章小编给大家分享一下python获取时间戳实现代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
在python 开发web程序时,需要调用第三方的相关接口,在调用时,需要对请求进行签名。需要用到unix时间戳。
在python里,得到的时间戳是10位。而java里默认是13位(milliseconds,毫秒级的)。
下面介绍python获得时间戳的方法:
1、10时间戳获取方法:
>>> import time >>> t = time.time() >>> print t 1436428326.76 >>> print int(t) 1436428326 >>>
强制转换是直接去掉小数位。
2、13位时间戳获取方法:
(1)默认情况下python的时间戳是以秒为单位输出的float
>>> >>> import time >>> time.time() 1436428275.207596 >>>
通过把秒转换毫秒的方法获得13位的时间戳:
import time millis = int(round(time.time() * 1000)) print millis
round()是四舍五入。
(2)
import time current_milli_time = lambda: int(round(time.time() * 1000)) Then: >>> current_milli_time() 1378761833768
13位时间 戳转换成时间:
>>> import time >>> now = int(round(time.time()*1000)) >>> now02 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now/1000)) >>> now02 '2017-11-07 16:47:14'
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27