subparse icon indicating copy to clipboard operation
subparse copied to clipboard

Add missing Timepoint Add (Sub exists)

Open mabushey opened this issue 2 years ago • 2 comments

Sub for TimePoint exists, but not Add:

49 |   let t2 = subtitle_entries[pos+3].timespan.end + TimePoint::from_secs(2);
   |                                                   ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TimeDelta`, found struct `TimePoint`

However we have impl Sub<TimeDelta> for TimePoint, so this works to add:

let t2 = subtitle_entries[pos+3].timespan.end - TimePoint::from_secs(-2);

This PR adds the Add for TimePoint, so now there is both add and sub.

The crate is awesome, thanks!

mabushey avatar Mar 01 '22 01:03 mabushey

A TimePoint is the equivalent to an "Instant" in std, TimeDelta to "Duration". You can not add the TimePoints or dates "next Tuesday" and "next Friday", but you can add or subtract a TimeDelta of "3 days".

"TimePoint minus TimePoint" is a TimeDelta. So "Monday + (Friday - Thursday) = Monday + 1day = Tuesday".

The idea behind this separation was to forbid the addition of TimePoints to avoid implementation mistakes. It might be a little bit overengineered :) Try using a TimeDelta for your application and see how it goes.

kaegi avatar Mar 01 '22 11:03 kaegi

I just copied the missing Add so we have Add and Sub. Right now I'm Subtracting a negative so it's working for my use but klunky. I'm making a short preview video with the subtitles baked in to test that the subtitles are aligned correct:

let t1 = subtitle_entries[pos].timespan.start - TimePoint::from_secs(2);
let t2 = subtitle_entries[pos+3].timespan.end - TimePoint::from_secs(-2);

I'm quite new to rust so please let me know if there's a better way.

mabushey avatar Mar 01 '22 16:03 mabushey