2016-06-10 89 views
4

我想從問題創建到解決問題的時間。隨着這些領域:吉拉蟒蛇計算時間

creation_time = issue.fields.created 
resolved_time = issue.fields.resolutiondate 

輸出當我打印:

Creation: 2016-06-09T14:37:05.000+0200 Resolved: 2016-06-10T10:53:12.000+0200 

反正是有,我可以減去分辨率日期和時間的創建日期和時間來找到多少時間是花在一個問題?

回答

4

將日期/時間字符串解析爲合適的日期時間對象,然後您可以使用它們進行計算。

This post解釋如何解析日期/時間字符串,或者你可以看看documentation for the strptime() method

爲了計算,在this post中有示例,並且有詳細的文檔here

作爲一個例子,這樣的事情應該是接近的解決方案:

from datetime import datetime 
from datetime import timedelta 

createdTime = datetime.strptime('2016-06-09T14:37:05.000+0200', '%Y-%m-%dT%H:%M:%S.%f') 
resolvedTime = datetime.strptime('2016-06-10T10:53:12.000+0200', '%Y-%m-%dT%H:%M:%S.%f') 

duration = resolvedTime - createdTime 

時間將是一個timedelta對象,你可以訪問duration.daysduration.secondsduration.microseconds以獲取其信息。

strptime確實有一個缺點,它不支持分析時區,所以您必須首先剪切輸入部分。或者,請參閱this post

+0

這對我來說會如何? – user3580316

+0

添加了一個應該接近您需要的示例。 – GlennV

+1

我收到這個錯誤'ValueError:'z'是格式爲'%Y-%m-%dT%H:%M:%S.%f%z'' Maye'dateutil'更糟的指令(http://stackoverflow.com/questions/6571419/python-parsing-a-datetime-with-a-timezone) – user3580316