python 2.6中total_seconds()的替代方法 [英] alternative to total_seconds() in python 2.6
本文介绍了python 2.6中total_seconds()的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 python 2.7 中有 total_seconds() 方法.
In python 2.7 there is the total_seconds() method.
在python 2.6中,它不存在,建议使用
In python 2.6, it doesn't exist and it suggests to use
(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
有人可以告诉我如何在下面实现它吗?
Could somebody show me how to implement it below?
谢谢
import datetime
timestamp = '2014-10-24 00:00:00'
timestamp = int((datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') - datetime.datetime(1970,1,1)).total_seconds())
print timestamp
timestamp = '2014-10-24 00:00:00'
timestamp = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') - datetime.datetime(1970,1,1)
print timestamp
推荐答案
这里是在 timedelta_total_seconds 函数中,使用 2.7 可以看到它获得与使用 total_seconds 方法相同的输出.
Here it is in a timedelta_total_seconds function and using 2.7 you can see that it gets the same output as using the total_seconds method.
import datetime
def timedelta_total_seconds(timedelta):
return (
timedelta.microseconds + 0.0 +
(timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6
timestamp = '2014-10-24 00:00:00'
time_delta = datetime.datetime.strptime(
timestamp, '%Y-%m-%d %H:%M:%S') - datetime.datetime(1970, 1, 1)
print timedelta_total_seconds(time_delta)
print time_delta.total_seconds()
输出
1414108800.0
1414108800.0
这篇关于python 2.6中total_seconds()的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文