首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python - 获取时间戳(10位和1

python - 获取时间戳(10位和1

作者头像
py3study
发布2020-01-08 17:01:52
5.8K0
发布2020-01-08 17:01:52
举报
文章被收录于专栏:python3python3

在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'
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档