@@ -144,6 +144,81 @@ class Timecode:
144144 def seconds (self ) -> float :
145145 return float (self .time_base * self .pts )
146146
147+ def _get_other_as_seconds (self , other : ty .Any ) -> float :
148+ if isinstance (other , Timecode ):
149+ return other .seconds
150+ if isinstance (other , float ):
151+ return float (other )
152+ raise TypeError (f"Unsupported type for comparison with Timecode: { type (other )} " )
153+
154+ def __add__ (self , other : ty .Union [float , "Timecode" ]) -> "Timecode" :
155+ if isinstance (other , Timecode ):
156+ if self .time_base != other .time_base :
157+ raise ValueError ("Timecode instances require equal time_base for arithmetic." )
158+ return Timecode (self .pts + other .pts , self .time_base )
159+ if isinstance (other , float ):
160+ # Assume other is in seconds. Convert to pts.
161+ pts_offset = round (other / self .time_base )
162+ return Timecode (self .pts + pts_offset , self .time_base )
163+ return NotImplemented
164+
165+ def __sub__ (self , other : ty .Union [float , "Timecode" ]) -> "Timecode" :
166+ if isinstance (other , Timecode ):
167+ if self .time_base != other .time_base :
168+ raise ValueError ("Timecode instances require equal time_base for arithmetic." )
169+ return Timecode (self .pts - other .pts , self .time_base )
170+ if isinstance (other , float ):
171+ # Assume other is in seconds. Convert to pts.
172+ pts_offset = round (other / self .time_base )
173+ return Timecode (self .pts - pts_offset , self .time_base )
174+ return NotImplemented
175+
176+ def __eq__ (self , other : ty .Any ) -> bool :
177+ try :
178+ return math .isclose (self .seconds , self ._get_other_as_seconds (other ))
179+ except TypeError :
180+ return NotImplemented
181+
182+ def __ne__ (self , other : ty .Any ) -> bool :
183+ eq_result = self .__eq__ (other )
184+ return not eq_result if eq_result is not NotImplemented else NotImplemented
185+
186+ def __lt__ (self , other : ty .Any ) -> bool :
187+ try :
188+ other_seconds = self ._get_other_as_seconds (other )
189+ if math .isclose (self .seconds , other_seconds ):
190+ return False
191+ return self .seconds < other_seconds
192+ except TypeError :
193+ return NotImplemented
194+
195+ def __le__ (self , other : ty .Any ) -> bool :
196+ try :
197+ other_seconds = self ._get_other_as_seconds (other )
198+ if math .isclose (self .seconds , other_seconds ):
199+ return True
200+ return self .seconds < other_seconds
201+ except TypeError :
202+ return NotImplemented
203+
204+ def __gt__ (self , other : ty .Any ) -> bool :
205+ try :
206+ other_seconds = self ._get_other_as_seconds (other )
207+ if math .isclose (self .seconds , other_seconds ):
208+ return False
209+ return self .seconds > other_seconds
210+ except TypeError :
211+ return NotImplemented
212+
213+ def __ge__ (self , other : ty .Any ) -> bool :
214+ try :
215+ other_seconds = self ._get_other_as_seconds (other )
216+ if math .isclose (self .seconds , other_seconds ):
217+ return True
218+ return self .seconds > other_seconds
219+ except TypeError :
220+ return NotImplemented
221+
147222
148223class FrameTimecode :
149224 """Object for frame-based timecodes, using the video framerate to compute back and
0 commit comments