python怎么生成时间戳


本文摘自php中文网,作者爱喝马黛茶的安东尼,侵删。

在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

>>>

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

相关推荐:《Python视频教程》

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

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

'2019-06-21 09:53:17'

以上就是python怎么生成时间戳的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python操作mysql模拟银行转账操作的简单实例

Python中的is和==比较两个对象的方法详解

Python中进程间数据通讯模块multiprocessing.manager的介绍

Python + wordcloud + jieba 十分钟学会生成中文词云

简单介绍Python编程中的字符串编码问题

Python模块和类区别

Python如何无视大小写

centos 6.5下安装Python 3.5.2

基于Python如何实现计算两组数据p值

Python怎么把数据框内数据写入数据库

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...