Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>sla_list</code> contains the 3 values you asked for, so <code>sla_list[1]</code> is the end_out value, while <code>start_out</code> is <code>sla_list[2]</code>.</p> <p>Simply apply your calculation to those two values:</p> <pre><code>timedelta = sla_list[1] - sla_list[2] timedelta = '%02d:%02d' % divmod(timedelta.total_seconds(), 60) </code></pre> <p>I replaced your <code>time.gmtime(timedelta)</code> and <code>strftime</code> dance with a simpler method using the <a href="http://docs.python.org/library/functions.html#divmod" rel="nofollow"><code>divmod()</code> function</a>; all you need to do is turn the total number of seconds into hours (seconds divided by 60) and remaining seconds (seconds modulo 60), after all.</p> <p>If you need sum the timedeltas per <code>service__name</code> entry, order by by that entry and use the <a href="http://docs.python.org/library/itertools.html#itertools.groupby" rel="nofollow"><code>itertools.groupby</code> callable</a> to calculate your sums quickly and efficiently:</p> <pre><code>from itertools import groupby from operator import itemgetter sla_list = self.object_list.filter(types__name="Unplanned").filter(service__sla=True).order_by('service__name').values_list('service__name', 'end_out', 'start_out') sla_deltas = {} for name, entries in groupby(sla_list, itemgetter(0)): sla_deltas[name] = sum(end - start for name, end, start in entries) </code></pre> <p>The <code>groupby</code> function gives you sub-lists with matching <code>sla__name</code> values, we simply calculate the <code>datetime.timedelta</code> for each entry and use <code>sum()</code> to calculate the total timedelta.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload