PHP头条
热点:

python获取时间戳如何实现 python获取时间戳实现代码


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'
  • 上一篇: 原神公测版32冒险等阶传说任务如何完成 原神公测版32冒险等阶传说任务完成攻略

  • 下一篇: Springboot2.x+Quartz分布式集群实现代码

www.phpzy.comtrue/php/40078.htmlTechArticlepython获取时间戳如何实现 python获取时间戳实现代码 python获取时间戳如何实现?本篇文章小编给大家分享一下python获取时间戳实现代码,小编觉得挺不错的,现在分享给大家供大家参考...

相关文章

    暂无相关文章

PHP之友评论

今天推荐