脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python获取时间戳的实现示例(10位和13位)

python获取时间戳的实现示例(10位和13位)

2020-09-24 00:18Saggitarxm Python

这篇文章主要介绍了python获取时间戳的实现示例(10位和13位),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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
>>>

强制转换是直接去掉小数位。

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
6
7
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
'2017-11-07 16:47:14'

到此这篇关于python获取时间戳的实现示例(10位和13位)的文章就介绍到这了,更多相关python获取时间戳内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/xuezhangjun0121/article/details/78083717

延伸 · 阅读

精彩推荐