【发布时间】:2018-02-24 01:44:19
【问题描述】:
我需要比较数据库中的两个时间戳。首先,我将一条记录保存为字典,然后保存另一条记录,然后比较这些字典中的日期时间组件并得到以下错误消息:
TypeError:timedelta 秒组件的类型不受支持:datetime.datetime
相关代码为:
import datetime
# got records from database, then fromated dictionary:
record_1 = {'timestamp': timestamp, 'x': x, 'y': y}
# latter on
record_2 = record_1
record_1 = {'timestamp': timestamp, 'x': x, 'y': y}
# Records prints:
# {'timestamp': datetime.datetime(2018, 2, 21, 6, 47, 38), 'x': 1, 'y': 2}
# {'timestamp': datetime.datetime(2018, 2, 16, 21, 59, 59), 'x': 3, 'y': 4}
# Respectively, error ocures in line:
difference = datetime.timedelta(record_1['timestamp'], record_2['timestamp'])
可能是什么问题?
【问题讨论】: