1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from timelinelib.calendar.gregorian.gregorian import GregorianDateTime
18 from timelinelib.calendar.gregorian.time import GregorianTime
22
23 """
24 Represents a period in time using a start and end time.
25
26 This is used both to store the time period for an event and for storing the
27 currently displayed time period in the GUI.
28 """
29
30 - def __init__(self, start_time, end_time):
32
33 @property
35 return self._start_time
36
37 @property
40
41 @property
43 return self._start_time, self._end_time
44
49
51 return not (self == other)
52
55
58
61
64
67
70
73
76
79
82
83 - def update(self, start_time, end_time,
84 start_delta=None, end_delta=None):
87
88 - def _update(self, start_time, end_time, start_delta=None, end_delta=None):
89 """
90 Change the time period data.
91
92 Optionally add the deltas to the times like this: time + delta.
93
94 If data is invalid, it will not be set, and a ValueError will be raised
95 instead. Data is invalid if or if the start time is larger than the end
96 time.
97 """
98 new_start = self._calc_new_time(start_time, start_delta)
99 new_end = self._calc_new_time(end_time, end_delta)
100 self._assert_period_is_valid(new_start, new_end)
101 return (new_start, new_end)
102
104 if new_start is None:
105 raise ValueError(_("Invalid start time"))
106 if new_end is None:
107 raise ValueError(_("Invalid end time"))
108 if new_start > new_end:
109 raise ValueError(_("Start time can't be after end time. Start:%s End:%s" %
110 (new_start.to_str(), new_end.to_str())))
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