1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 -class TimePeriod(object):
20
21 """
22 Represents a period in time using a start and end time.
23
24 This is used both to store the time period for an event and for storing the
25 currently displayed time period in the GUI.
26 """
27
28 - def __init__(self, start_time, end_time):
30
31 @property
33 return self._start_time
34
35 @property
38
39 @property
41 return self._start_time, self._end_time
42
47
49 return not (self == other)
50
53
56
59
62
65
68
71
74
77
80
81 - def update(self, start_time, end_time,
82 start_delta=None, end_delta=None):
85
86 - def _update(self, start_time, end_time, start_delta=None, end_delta=None):
87 """
88 Change the time period data.
89
90 Optionally add the deltas to the times like this: time + delta.
91
92 If data is invalid, it will not be set, and a ValueError will be raised
93 instead. Data is invalid if or if the start time is larger than the end
94 time.
95 """
96 new_start = self._calc_new_time(start_time, start_delta)
97 new_end = self._calc_new_time(end_time, end_delta)
98 self._assert_period_is_valid(new_start, new_end)
99 return (new_start, new_end)
100
102 if new_start is None:
103 raise ValueError(_("Invalid start time"))
104 if new_end is None:
105 raise ValueError(_("Invalid end time"))
106 if new_start > new_end:
107 raise ValueError(_("Start time can't be after end time. Start:%s End:%s" %
108 (new_start.to_str(), new_end.to_str())))
109
111 """
112 Return True if the given time is inside this period or on the border,
113 otherwise False.
114 """
115 return time >= self.start_time and time <= self.end_time
116
124
128
132
135
138
141
144
147
150
152 """
153 Return True if this time period is longer than just a point in time,
154 otherwise False.
155 """
156 return self.start_time != self.end_time
157
159 """
160 Return the time in the middle if this time period is longer than just a
161 point in time, otherwise the point in time for this time period.
162 """
163 return self.start_time + (self.delta() / 2)
164
165 - def zoom(self, times, ratio=0.5):
166 start_delta = self.delta() * (times * ratio / 5.0)
167 end_delta = self.delta() * (-times * (1.0 - ratio) / 5.0)
168 return self.update(self.start_time, self.end_time, start_delta, end_delta)
169
170 - def move(self, direction):
171 """
172 Move this time period one 10th to the given direction.
173
174 Direction should be -1 for moving to the left or 1 for moving to the
175 right.
176 """
177 delta = self.delta() * (direction / 10.0)
178 return self.move_delta(delta)
179
182
184 """Return the length of this time period as a timedelta object."""
185 return self.end_time - self.start_time
186
189
194
198
202
205 """
206 TimePeriod factory method.
207
208 Return a time period with the given length (represented as a timedelta)
209 centered around `time`.
210 """
211 half_length = length * 0.5
212 start_time = time - half_length
213 end_time = time + half_length
214 return TimePeriod(start_time, end_time)
215