5

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?

3 Answers3

3

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.

Community
  • 1
  • 1
GlennV
  • 3,471
  • 4
  • 26
  • 39
  • How will that be in my case? –  Jun 10 '16 at 13:14
  • Added an example that should be close to what you need. – GlennV Jun 10 '16 at 13:37
  • 1
    I receive this error `ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%f%z'` Maye `dateutil` is more sufficient to use(http://stackoverflow.com/questions/6571419/python-parsing-a-datetime-with-a-timezone) –  Jun 10 '16 at 17:33
  • Turns out strptime doesn't handle timezones. I've updated the answer and added another link to a post with a solution. – GlennV Jun 11 '16 at 15:19
1

strptime does not support parsing timezones. This code is working for me

from datetime import datetime
createdTime = datetime.strptime(issue.fields.created.split(".")[0], '%Y-%m-%dT%H:%M:%S')
resolvedTime = datetime.strptime(issue.fields.resolutiondate.split(".")[0], '%Y-%m-%dT%H:%M:%S')
duration = resolvedTime - createdTime
saibhaskar
  • 435
  • 6
  • 21
0

I've write a function which calculates mean, median and variance of the respond times in days. Hope that helps;

import datetime as d
import numpy as np

ymd_create = []
ymd_resdate = []
delta_t = []

class calculate:
    def __init__(self):
        self.result = 0

    def meantime(self, issueobject):
        for i in range(0, len(issueobject)):
            ymd_create.append(d.datetime(int(issueobject[i].raw[u'fields'][u'created'].split('T')[0].split('-')[0]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[0].split('-')[1]), int(issueobject[i].raw[u'fields']
                                                                                                                                                                                                     [u'created'].split('T')[0].split('-')[2]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[1].split(':')[0]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[1].split(':')[1])))
            ymd_resdate.append(d.datetime(int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[0].split('-')[0]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[0].split('-')[1]), int(issueobject[i].raw[u'fields']
                                                                                                                                                                                                                    [u'resolutiondate'].split('T')[0].split('-')[2]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[1].split(':')[0]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[1].split(':')[1])))
            delta_t.append((ymd_resdate[i] - ymd_create[i]).days)

        self.result = np.mean(np.array(delta_t))
        return self.result

    def mediantime(self, issueobject):
        for i in range(0, len(issueobject)):
            ymd_create.append(d.datetime(int(issueobject[i].raw[u'fields'][u'created'].split('T')[0].split('-')[0]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[0].split('-')[1]), int(issueobject[i].raw[u'fields']
                                                                                                                                                                                                     [u'created'].split('T')[0].split('-')[2]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[1].split(':')[0]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[1].split(':')[1])))
            ymd_resdate.append(d.datetime(int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[0].split('-')[0]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[0].split('-')[1]), int(issueobject[i].raw[u'fields']
                                                                                                                                                                                                                    [u'resolutiondate'].split('T')[0].split('-')[2]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[1].split(':')[0]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[1].split(':')[1])))
            delta_t.append((ymd_resdate[i] - ymd_create[i]).days)

        self.result = np.median(np.array(delta_t))
        return self.result

    def variancetime(self, issueobject):
        for i in range(0, len(issueobject)):
            ymd_create.append(d.datetime(int(issueobject[i].raw[u'fields'][u'created'].split('T')[0].split('-')[0]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[0].split('-')[1]), int(issueobject[i].raw[u'fields']
                                                                                                                                                                                                     [u'created'].split('T')[0].split('-')[2]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[1].split(':')[0]), int(issueobject[i].raw[u'fields'][u'created'].split('T')[1].split(':')[1])))
            ymd_resdate.append(d.datetime(int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[0].split('-')[0]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[0].split('-')[1]), int(issueobject[i].raw[u'fields']
                                                                                                                                                                                                                    [u'resolutiondate'].split('T')[0].split('-')[2]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[1].split(':')[0]), int(issueobject[i].raw[u'fields'][u'resolutiondate'].split('T')[1].split(':')[1])))
            delta_t.append((ymd_resdate[i] - ymd_create[i]).days)

        self.result = np.var(np.array(delta_t))
        return self.result
Capan
  • 686
  • 1
  • 14
  • 32