我有一堆datetime对象,我想为每个对象计算从过去的固定时间(例如从1970年1月1日以来)开始的秒数。

import datetime
t = datetime.datetime(2009, 10, 21, 0, 0)

这似乎只是区分有不同日期的日期:

t.toordinal()

如何将datetime对象转换为秒?


当前回答

在python 3中,以毫秒为单位计算一个代码块的处理时间的标准方法。X表示:

import datetime

t_start = datetime.datetime.now()

# Here is the python3 code, you want 
# to check the processing time of

t_end = datetime.datetime.now()
print("Time taken : ", (t_end - t_start).total_seconds()*1000, " ms")

其他回答

Int (t.strftime("%s"))也可以

Python提供了datetime操作来计算两个日期之间的差值。在你的情况下,这将是:

t - datetime.datetime(1970,1,1)

返回的值是一个timedelta对象,您可以使用成员函数total_seconds获取以秒为单位的值。

(t - datetime.datetime(1970,1,1)).total_seconds()

获取Unix时间(从1970年1月1日开始的秒数):

>>> import datetime, time
>>> t = datetime.datetime(2011, 10, 21, 0, 0)
>>> time.mktime(t.timetuple())
1319148000.0

比较4种最常见的方法,以确保准确性:

方法一:手动计算

from datetime import datetime
total1 = int(datetimeobj.strftime('%S'))
total1 += int(datetimeobj.strftime('%M')) * 60
total1 += int(datetimeobj.strftime('%H')) * 60 * 60
total1 += (int(datetimeobj.strftime('%j')) - 1) * 60 * 60 * 24
total1 += (int(datetimeobj.strftime('%Y')) - 1970) * 60 * 60 * 24 * 365
print ("Method #1: Manual")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total1)
print ("After: %s" % datetime.fromtimestamp(total1))

输出:

Method #1: Manual
Before: 1970-10-01 12:00:00 
Seconds: 23630400 
After: 1970-10-01 16:00:00

精度测试:FAIL(时区偏移)

方法二:时间模块

import time
from datetime import datetime
total2 = int(time.mktime(datetimeobj.timetuple()))
print ("Method #2: Time Module")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total2)
print ("After: %s" % datetime.fromtimestamp(total2))

输出:

Method #2: Time Module
Before: 1970-10-01 12:00:00 
Seconds: 23616000 
After: 1970-10-01 12:00:00

准确度测试:通过

方法三:日历模块

import calendar
from datetime import datetime
total3 = calendar.timegm(datetimeobj.timetuple())
print ("Method #3: Calendar Module")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total3)
print ("After: %s" % datetime.fromtimestamp(total3))

输出:

Method #3: Calendar Module
Before: 1970-10-01 12:00:00
Seconds: 23616000
After: 1970-10-01 16:00:00

精度测试:FAIL(时区偏移)

方法四:Datetime时间戳

from datetime import datetime
total4 = datetimeobj.timestamp()
print ("Method #4: datetime timestamp")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total4)
print ("After: %s" % datetime.fromtimestamp(total4))

输出:

Method #2: Time Module
Before: 1970-10-01 12:00:00 
Seconds: 23616000 
After: 1970-10-01 12:00:00

准确度测试:通过

结论

所有4个方法都将datetime转换为epoch(总秒数) Manual方法和Calendar模块方法都是时区感知的。 datetime.timestamp()和time.mktime()方法都不涉及时区。 最简单的方法:datetime.timestamp()

我并没有在所有的答案中看到这一点,尽管我猜这是默认的需求:

t_start = datetime.now()
sleep(2)
t_end = datetime.now()
duration = t_end - t_start
print(round(duration.total_seconds()))

如果不使用.total_seconds(),则抛出:TypeError: type datetime。Timedelta没有定义__round__方法。

例子:

>>> duration
datetime.timedelta(seconds=53, microseconds=621861)
>>> round(duration.total_seconds())
54
>>> duration.seconds
53

持续时间。Seconds只使用秒,不考虑微秒,就像运行math.floor(duration.total_seconds())一样。