Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't see where is the problem, apart in case the end-time would be later than 24 hours after the start-time.</p> <p>Suppose that start-time is 9:00:00 and end-time is 13:00:00<br> If these times were taken on August 15, 9:00:00 and August 17, 13:00:00 , there would be no sense to try to obtain the time delta between them without knowing the days 15 and 17.</p> <p>Hence there are two cases: </p> <ul> <li><p>either the start time and end time may be really separated by more than 24 hours , then as it has already been said, you must move to the use of datetime's objects</p></li> <li><p>either there is always less than 24 hours between the start-time and the end-time, then the problem is simple.</p></li> </ul> <p>==========================</p> <p>Let us examine the second case.</p> <p>If<br> start-time 11:30:00<br> end-time.. 12:35:00<br> The end is evidently <em>1 hour 5 minutes</em> after the start</p> <p>If<br> start-time 11:30:00<br> end-time.. 10:35:00<br> The end can't be before the start in the same morning, then the end is in fact in the morning of the next day after the day in which is the start, that is to say 24 hours later.</p> <p>The same reasoning applies when the start is in the afternoon and the end time is apparently before the start time in the same day, in afternoon or morning: end time is in fact in the the next day, morning or afternoon, it depends but it's still 24 hours later.</p> <h3>1)</h3> <p>So a little function, that need only the attributes of the times is sufficient to deduct the time difference:</p> <pre><code>def difft(start,end): a,b,c,d = start.hour, start.minute, start.second, start.microsecond w,x,y,z = end.hour, end.minute, end.second, end.microsecond delt = (w-a)*60 + (x-b) + (y-c)/60. + (z-d)/60000000 return delt + 1440 if delt&lt;0 else delt </code></pre> <p>The following code is only for a better display of the result:</p> <p>from datetime import time</p> <pre><code>def difft(start,end): a,b,c,d = start.hour, start.minute, start.second, start.microsecond w,x,y,z = end.hour, end.minute, end.second, end.microsecond delt = (w-a)*60 + (x-b) + (y-c)/60. + (z-d)/60000000 D = '%sh %smn %ss %sms - %sh %smn %ss %sms == ' ft = '%s + 1440 = %s (1440 = 24x60mn)' return D % (w,x,y,z,a,b,c,d) +( ft % (delt, delt+1440) if delt&lt;0 else str(delt)) print difft(time(11,30,0),time(12,35,0)) print difft(time(11,30,0),time(10,35,0)) print print difft(time(20,40,0),time(22,41,0)) print difft(time(20,40,0),time(18,41,0)) </code></pre> <p>result</p> <pre><code>12h 35mn 0s 0ms - 11h 30mn 0s 0ms == 65.0 10h 35mn 0s 0ms - 11h 30mn 0s 0ms == -55.0 + 1440 = 1385.0 (1440 = 24x60mn) 22h 41mn 0s 0ms - 20h 40mn 0s 0ms == 121.0 18h 41mn 0s 0ms - 20h 40mn 0s 0ms == -119.0 + 1440 = 1321.0 (1440 = 24x60mn) </code></pre> <h3>2)</h3> <p>To obtain the durations in a more readable format:</p> <pre><code>def difft2(start,end): a,b,c,d = start.hour, start.minute, start.second, start.microsecond w,x,y,z = end.hour, end.minute, end.second, end.microsecond delt = (w-a)*60 + (x-b) + (y-c)/60. + (z-d)/60000000. if delt &lt; 0: delt += 1440 hh,rem = divmod(delt,60) hh = int(hh) mm = int(rem) rem = (rem - mm)*60 ss = int(rem) ms = (rem - ss)*1000000 ms = int(ms) SS = '%sh %smn %ss %sms - %sh %smn %ss %sms == %sh %smn %ss %sms' return SS % (w,x,y,z,a,b,c,d,hh,mm,ss,ms) print difft2(time(11,30,0),time(12,35,45,478129)) print difft2(time(11,30,45,11),time(10,35,45,12)) print print difft2(time(20,40,0),time(22,41,0)) print difft2(time(20,40,0),time(18,41,0)) </code></pre> <p>result</p> <pre><code>12h 35mn 45s 478129ms - 11h 30mn 0s 0ms == 1h 5mn 45s 478128ms 10h 35mn 45s 12ms - 11h 30mn 45s 11ms == 23h 5mn 0s 1ms 22h 41mn 0s 0ms - 20h 40mn 0s 0ms == 2h 1mn 0s 0ms 18h 41mn 0s 0ms - 20h 40mn 0s 0ms == 22h 1mn 0s 0ms </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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