Search code examples
pythonjirapython-jira

Jira python calculate time


I am trying to calculate the time from the issue is created and until it is resolved. With these fields:

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

Output when I print:

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

Is there anyway I can minus the resolution date and time with the creation date and time to find how much time is spent on a issue?


Solution

  • Parse the date/time strings into a suitable datetime object and then you can use those to do calculations.

    This post explains how to parse a date/time string or you can just take a look at the documentation for the strptime() method.

    For calculations, there are examples in this post and there's detailed documentation here.

    As an example, something like this should be close to a solution:

    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
    

    duration will be a timedelta object and you can access duration.days, duration.seconds and duration.microseconds to get its info.

    strptime does have as a drawback that it does not support parsing timezones, so you'll have to cut that part of your input first. Alternatively, see this post.