Calculating issue duration in Python using Jira API

Hey everyone, I’m working on a Python script to figure out how long it takes to resolve issues in Jira. I’ve got the creation and resolution timestamps, but I’m stuck on how to calculate the time difference. Here’s what I have so far:

issue_start = jira_issue.fields.created
issue_end = jira_issue.fields.resolutiondate

print(f'Started: {issue_start}')
print(f'Finished: {issue_end}')

This gives me output like:

Started: 2023-05-15T09:22:30.000+0100
Finished: 2023-05-16T16:45:18.000+0100

What’s the best way to find the time difference between these two dates? I’ve tried a few things but can’t seem to get it right. Any help would be awesome!

hey there! i’ve dealt with this before. you can use the datetime module to parse those timestamps and then subtract them. something like:

from datetime import datetime

start = datetime.strptime(issue_start, ‘%Y-%m-%dT%H:%M:%S.%f%z’)
end = datetime.strptime(issue_end, ‘%Y-%m-%dT%H:%M:%S.%f%z’)
duration = end - start

print(f’Duration: {duration}')

To calculate the duration between the two timestamps, you can utilize the dateutil library, which handles timezone information more gracefully than the standard datetime module. Here’s how you can implement it:

from dateutil import parser

issue_start = parser.parse(jira_issue.fields.created)
issue_end = parser.parse(jira_issue.fields.resolutiondate)

duration = issue_end - issue_start

print(f'Duration: {duration}')

This approach automatically handles the timezone information present in your timestamps. The resulting duration will be a timedelta object, which you can further format or extract specific information from as needed for your analysis.

I’ve been using Jira for a while now, and I’ve found that the simplest way to handle this is with the dateutil library. It’s really handy for parsing those ISO format dates that Jira spits out. Here’s what I usually do:

from dateutil import parser
from datetime import timedelta

start = parser.parse(issue_start)
end = parser.parse(issue_end)
duration = end - start

# Round to nearest minute
rounded_duration = timedelta(minutes=round(duration.total_seconds() / 60))

print(f'Issue took approximately {rounded_duration}')

This gives you a nice, human-readable output. You can adjust the rounding if you need more precision. Just remember to pip install python-dateutil if you haven’t already. Hope this helps!