我有一个城市的通用交通提要规范数据数据库,将午夜后的交通服务定义为小时 > 24。因此,在 stop_times 表中,我们定义了很多次,例如 25:00:00、26:00:00等。由于我需要对这个数据库的一部分执行时间减法,我想我会编写一个用户定义的 python 脚本来处理这个问题,并使用 python create_function sqlite 命令将它与我的数据库相关联。
出于某种原因,当我在这个数据集上运行我想到的查询时,我得到
sqlite3.OperationalError: user-defined function raised exception
这是我为处理午夜之后的时间而编写的时间减法函数。我敢肯定这是一团糟。如果您对如何更有效地处理此问题有任何提示,我也很想听听。提前致谢。
def time_delta(t1, t2):
old_arrival = t1.encode('utf-8').split(':')
old_departure = t2.encode('utf-8').split(':')
new_arrival_string = "2013-03-16 %s:%s:%s" % (int(old_arrival[0])-12, old_arrival[1], old_arrival[2])
new_arrival_format = "%Y-%m-%d %H:%M:%S"
arr = datetime.datetime.strptime(new_arrival_string, new_arrival_format)
new_departure_string = "2013-03-16 %s:%s:%s" % (int(old_departure[0])-12, old_departure[1], old_departure[2])
new_departure_format = "%Y-%m-%d %H:%M:%S"
dep = datetime.datetime.strptime(new_departure_string, new_departure_format)
difference = arr-dep
seconds = difference.seconds
if difference.days < 0:
difference = dep-arr
seconds = (-1) * difference.seconds
return seconds