python接口自动化测试三十三:获取时间戳(10位和13位)

很多时候,在调用接口时,需要对请求进行签名。需要用到unix时间戳。

在python里,在网上介绍的很多方法,得到的时间戳是10位。而java里默认是13位(milliseconds,毫秒级的)。

下面介绍python获得时间戳的方法:

1、10位时间戳获取方法:强制转换是直接去掉小数位。

import time
a = time.time()
print(a)
print(int(a))

1554949545.1507404
1554949545

2、13位时间戳获取方法:

默认情况下python的时间戳是以秒为单位输出的float

通过把秒转换毫秒的方法获得13位的时间戳:round()是四舍五入。

import time
b = time.time()
c = int(round(b * 1000))
print(c)

1554949730014

13位时间 戳转换成时间:

import time
now = int(round(time.time()*1000))
print(now)
now2 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now/1000))
print(now2)

1554949923557
2019-04-11 10:32:03

猜你喜欢

转载自www.cnblogs.com/zhongyehai/p/10688102.html
今日推荐