使用timedelta计算两次ISO8601次之间的差值

时间:2018-05-09 19:09:52

标签: python datetime timedelta

我正在尝试计算两个ISO8601格式化时间之间的增量。我有兴趣保留时间标准中的6个有效数字。我发现了“timedelta”功能,但无法让它正常工作。

duplicate key value violates unique constraint "cashflow_cashflow_pkey"
DETAIL:  Key (id)=(4852) already exists.

Exception Type: IntegrityError
Exception Value:    duplicate key value violates unique constraint "cashflow_cashflow_pkey"
DETAIL:  Key (id)=(4852) already exists.

如何计算这两次到毫秒之间的差异?

编辑: 我的实际代码如下:

SELECT setval('user_id_seq', (SELECT MAX(id) FROM user));

1 个答案:

答案 0 :(得分:1)

作为答案(暂时至少)发布,因为我需要代码格式化的空间。如果我运行你在python终端中提供的代码,一切看起来都按预期工作:

% python
Python 2.7.8 (default, Apr 25 2018, 00:29:19) 
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> command = "NAME,COMMAND,2018-05-10T18:31:30.515276"
>>> command_lis = command.replace("/n"," ").split(",")
>>> print("Received the following time: " + command_lis[2])
Received the following time: 2018-05-10T18:31:30.515276
>>> a = datetime.datetime.strptime(datetime.datetime.utcnow().isoformat(), '%Y-%m-%dT%H:%M:%S.%f')
>>> print("a is set")
a is set
>>> b = datetime.datetime.strptime(command_lis[2], '%Y-%m-%dT%H:%M:%S.%f')
>>> print("b is set")
b is set
>>> delta = b - a
>>> print(delta)
-5 days, 18:25:54.502559

你有不同的行为吗?