1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from timelinelib.calendar.gregorian.gregorian import GregorianDateTime
21
22 """
23 Represents a period in time using a start and end time.
24
25 This is used both to store the time period for an event and for storing the
26 currently displayed time period in the GUI.
27 """
28
29 - def __init__(self, start_time, end_time):
31
32 @property
34 return self._start_time
35
36 @property
39
40 @property
42 return self._start_time, self._end_time
43
48
50 return not (self == other)
51
54
57
60
63
66
69
72
75
78
81
82 - def update(self, start_time, end_time,
83 start_delta=None, end_delta=None):
86
87 - def _update(self, start_time, end_time, start_delta=None, end_delta=None):
88 """
89 Change the time period data.
90
91 Optionally add the deltas to the times like this: time + delta.
92
93 If data is invalid, it will not be set, and a ValueError will be raised
94 instead. Data is invalid if or if the start time is larger than the end
95 time.
96 """
97 new_start = self._calc_new_time(start_time, start_delta)
98 new_end = self._calc_new_time(end_time, end_delta)
99 self._assert_period_is_valid(new_start, new_end)
100 return (new_start, new_end)
101
103 if new_start is None:
104 raise ValueError(_("Invalid start time"))
105 if new_end is None:
106 raise ValueError(_("Invalid end time"))
107 if new_start > new_end:
108 raise ValueError(_("Start time can't be after end time. Start:%s End:%s" %
109 (GregorianDateTime.from_time(new_start),
110 GregorianDateTime.from_time(new_end))))
111
113 """
114 Return True if the given time is inside this period or on the border,
115 otherwise False.
116 """
117 return time >= self.start_time and time <= self.end_time
118
126
130
134
137
140
143
146
149
152
154 """
155 Return True if this time period is longer than just a point in time,
156 otherwise False.
157 """
158 return self.start_time != self.end_time
159
161 """
162 Return the time in the middle if this time period is longer than just a
163 point in time, otherwise the point in time for this time period.
164 """
165 return self.start_time + (self.delta() / 2)
166
167 - def zoom(self, times, ratio=0.5):
168 start_delta = self.delta() * (times * ratio / 5.0)
169 end_delta = self.delta() * (-times * (1.0 - ratio) / 5.0)
170 return self.update(self.start_time, self.end_time, start_delta, end_delta)
171
172 - def move(self, direction):
173 """
174 Move this time period one 10th to the given direction.
175
176 Direction should be -1 for moving to the left or 1 for moving to the
177 right.
178 """
179 delta = self.delta() * (direction / 10.0)
180 return self.move_delta(delta)
181
184
186 """Return the length of this time period as a timedelta object."""
187 return self.end_time - self.start_time
188
191
196
200
204
207 """
208 TimePeriod factory method.
209
210 Return a time period with the given length (represented as a timedelta)
211 centered around `time`.
212 """
213 half_length = length * 0.5
214 start_time = time - half_length
215 end_time = time + half_length
216 return TimePeriod(start_time, end_time)
217