如何从Python中gmtime()的时间+日期输出的时代获取秒数? [英] How to get the seconds since epoch from the time + date output of gmtime() in Python?

查看:465
本文介绍了如何从Python中gmtime()的时间+日期输出的时代获取秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,你如何反转 gmtime(),你放在时间+日期并得到秒数?

In python, how do you do reverse gmtime(), where you put the time + date and get the number of seconds?

我有字符串,如2009年7月9日@ 20:02:58 UTC,我想恢复2009年7月9日之间的秒数。

I have strings like 'Jul 9, 2009 @ 20:02:58 UTC', and i want to get back the number of seconds between the epoch and July 9, 2009.

我尝试过 time.strftime ,但我不知道如何正确使用它,或者是正确的使用命令。

I have tried time.strftime but I don't know how to use it properly, or if it is the correct command to use.

推荐答案

您想要 calendar.timegm()

>>> calendar.timegm(time.gmtime())
1293581619.0

你可以把你的字符串变成一个带有 time.strptime()的时间元组,它返回一个可以传递到 calendar.timegm():

You can turn your string into a time tuple with time.strptime(), which returns a time tuple that you can pass to calendar.timegm():

>>> import calendar
>>> import time
>>> calendar.timegm(time.strptime('Jul 9, 2009 @ 20:02:58 UTC', '%b %d, %Y @ %H:%M:%S UTC'))
1247169778

有关日历模块的更多信息这里

More information about calendar module here

这篇关于如何从Python中gmtime()的时间+日期输出的时代获取秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆