annotate timedelta.py @ 0:78a59d65bfcc

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