Mercurial > hg > timedelta
comparison timedelta.py @ 0:78a59d65bfcc
add timedelta
author | Dirk Olmes <dirk@xanthippe.ping.de> |
---|---|
date | Sat, 10 Jan 2009 03:50:20 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78a59d65bfcc |
---|---|
1 | |
2 from datetime import time | |
3 | |
4 def __toMicroseconds(time): | |
5 result = time.microsecond | |
6 result += time.second * 1000000 | |
7 result += time.minute * 60 * 1000000 | |
8 result += time.hour * 60 * 60 * 1000000 | |
9 return result | |
10 | |
11 def timedelta(t1, t2): | |
12 """Calculate the difference between to datetime.time objects | |
13 | |
14 The result is returned as a time object as the datetime.timedelta does not provide | |
15 the same timeunits (hour, minute etc.) as the time objects that come as input to | |
16 this routine. | |
17 | |
18 This method always returns a positive difference, i.e. the order in which the two | |
19 time objects are passed does not matter. | |
20 """ | |
21 min = t1 | |
22 max = t2 | |
23 if t2 < t1: | |
24 min = t2 | |
25 max = t1 | |
26 | |
27 total1 = __toMicroseconds(min) | |
28 total2 = __toMicroseconds(max) | |
29 | |
30 difference = total2 - total1 | |
31 | |
32 hours = ((difference / 1000000) / 60) / 60 | |
33 difference -= hours * 60 * 60 * 1000000 | |
34 | |
35 minutes = (difference / 1000000) / 60 | |
36 difference -= minutes * 60 * 1000000 | |
37 | |
38 seconds = difference / 1000000 | |
39 difference -= seconds* 1000000 | |
40 | |
41 micro = difference | |
42 return time(hour=hours, minute=minutes, second=seconds, microsecond=micro) | |
43 |