1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.



use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use std::ops::{Add, AddAssign, Neg, Sub, SubAssign};

/// Represents a timepoint (e.g. start timepoint of a subtitle line).
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Timing(i64 /* number of milliseconds */);

/// The internal timing in `TimePoint` and `TimeDelta` (with all necessary functions and nice Debug information, etc.).
impl Timing {
    fn from_components(hours: i64, mins: i64, secs: i64, ms: i64) -> Timing {
        Timing(ms + 1000 * (secs + 60 * (mins + 60 * hours)))
    }

    fn from_msecs(ms: i64) -> Timing {
        Timing(ms)
    }

    fn from_csecs(cs: i64) -> Timing {
        Timing(cs * 10)
    }

    fn from_secs(s: i64) -> Timing {
        Timing(s * 1000)
    }

    fn from_mins(mins: i64) -> Timing {
        Timing(mins * 1000 * 60)
    }

    fn from_hours(h: i64) -> Timing {
        Timing(h * 1000 * 60 * 60)
    }

    fn msecs(&self) -> i64 {
        self.0
    }

    fn csecs(&self) -> i64 {
        self.0 / 10
    }

    fn secs(&self) -> i64 {
        self.0 / 1000
    }

    fn secs_f64(&self) -> f64 {
        self.0 as f64 / 1000.0
    }

    fn mins(&self) -> i64 {
        self.0 / (60 * 1000)
    }

    fn hours(&self) -> i64 {
        self.0 / (60 * 60 * 1000)
    }

    fn mins_comp(&self) -> i64 {
        self.mins() % 60
    }

    fn secs_comp(&self) -> i64 {
        self.secs() % 60
    }

    fn csecs_comp(&self) -> i64 {
        self.csecs() % 100
    }

    fn msecs_comp(&self) -> i64 {
        self.msecs() % 1000
    }

    fn is_negative(&self) -> bool {
        self.0 < 0
    }
}


impl Debug for Timing {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "Timing({})", self.to_string())
    }
}

impl Display for Timing {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        let t = if self.0 < 0 { -*self } else { *self };
        write!(
            f,
            "{}{}:{:02}:{:02}.{:03}",
            if self.0 < 0 { "-" } else { "" },
            t.hours(),
            t.mins_comp(),
            t.secs_comp(),
            t.msecs_comp()
        )
    }
}

impl Add for Timing {
    type Output = Timing;
    fn add(self, rhs: Timing) -> Timing {
        Timing(self.0 + rhs.0)
    }
}

impl Sub for Timing {
    type Output = Timing;
    fn sub(self, rhs: Timing) -> Timing {
        Timing(self.0 - rhs.0)
    }
}

impl AddAssign for Timing {
    fn add_assign(&mut self, r: Timing) {
        self.0 += r.0;
    }
}

impl SubAssign for Timing {
    fn sub_assign(&mut self, r: Timing) {
        self.0 += r.0;
    }
}

impl Neg for Timing {
    type Output = Timing;
    fn neg(self) -> Timing {
        Timing(-self.0)
    }
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
/// Represents a time point like the start time of a subtitle entry.
pub struct TimePoint {
    /// The internal timing (with all necessary functions and nice Debug information, etc.).
    intern: Timing,
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
/// Represents a duration between two `TimePoints`.
pub struct TimeDelta {
    /// The internal timing (with all necessary functions and nice Debug information, etc.).
    intern: Timing,
}

macro_rules! create_time_type {
    ($i:ident) => {

        impl $i {
            fn new(t: Timing) -> $i {
                $i { intern: t }
            }

            /// Create this time type from all time components.
            ///
            /// The components can be negative and/or exceed the its natural limits without error.
            /// For example `from_components(0, 0, 3, -2000)` is the same as `from_components(0, 0, 1, 0)`.
            pub fn from_components(hours: i64, mins: i64, secs: i64, ms: i64) -> $i {
                Self::new(Timing::from_components(hours, mins, secs, ms))
            }

            /// Create the time type from a given number of milliseconds.
            pub fn from_msecs(ms: i64) -> $i {
                Self::new(Timing::from_msecs(ms))
            }

            /// Create the time type from a given number of hundreth seconds (10 milliseconds).
            pub fn from_csecs(ms: i64) -> $i {
                Self::new(Timing::from_csecs(ms))
            }

            /// Create the time type with a given number of seconds.
            pub fn from_secs(ms: i64) -> $i {
                Self::new(Timing::from_secs(ms))
            }

            /// Create the time type with a given number of minutes.
            pub fn from_mins(mins: i64) -> $i {
                Self::new(Timing::from_mins(mins))
            }

            /// Create the time type with a given number of hours.
            pub fn from_hours(mins: i64) -> $i {
                Self::new(Timing::from_hours(mins))
            }

            /// Get the total number of milliseconds.
            pub fn msecs(&self) -> i64 {
                self.intern.msecs()
            }

            /// Get the total number of hundreth seconds.
            pub fn csecs(&self) -> i64 {
                self.intern.csecs()
            }

            /// Get the total number of seconds.
            pub fn secs(&self) -> i64 {
                self.intern.secs()
            }

            /// Get the total number of seconds.
            pub fn secs_f64(&self) -> f64 {
                self.intern.secs_f64()
            }

            /// Get the total number of seconds.
            pub fn mins(&self) -> i64 {
                self.intern.mins()
            }

            /// Get the total number of hours.
            pub fn hours(&self) -> i64 {
                self.intern.hours()
            }

            /// Get the milliseconds component in a range of [0, 999].
            pub fn msecs_comp(&self) -> i64 {
                self.intern.msecs_comp()
            }

            /// Get the hundreths seconds component in a range of [0, 99].
            pub fn csecs_comp(&self) -> i64 {
                self.intern.csecs_comp()
            }

            /// Get the seconds component in a range of [0, 59].
            pub fn secs_comp(&self) -> i64 {
                self.intern.secs_comp()
            }

            /// Get the minute component in a range of [0, 59].
            pub fn mins_comp(&self) -> i64 {
                self.intern.mins_comp()
            }

            /// Return `true` if the represented time is negative.
            pub fn is_negative(&self) -> bool {
                self.intern.is_negative()
            }

            /// Return the absolute value of the current time.
            pub fn abs(&self) -> $i {
                if self.is_negative() { -*self } else { *self }
            }
        }

        impl Neg for $i {
            type Output = $i;
            fn neg(self) -> $i {
                $i::new(-self.intern)
            }
        }

        impl Display for $i {
            fn fmt(&self, f: &mut Formatter) -> FmtResult {
                write!(f, "{}", self.intern)
            }
        }
    }
}

create_time_type!{TimePoint}
create_time_type!{TimeDelta}

macro_rules! impl_add {
    ($a:ty, $b:ty, $output:ident) => {
        impl Add<$b> for $a {
            type Output = $output;
            fn add(self, rhs: $b) -> $output {
                $output::new(self.intern + rhs.intern)
            }
        }
    }
}

macro_rules! impl_sub {
    ($a:ty, $b:ty, $output:ident) => {
        impl Sub<$b> for $a {
            type Output = $output;
            fn sub(self, rhs: $b) -> $output {
                $output::new(self.intern - rhs.intern)
            }
        }
    }
}

macro_rules! impl_add_assign {
    ($a:ty, $b:ty) => {
        impl AddAssign<$b> for $a {
            fn add_assign(&mut self, r: $b) {
                self.intern += r.intern;
            }
        }
    }
}

macro_rules! impl_sub_assign {
    ($a:ty, $b:ty) => {
        impl SubAssign<$b> for $a {
            fn sub_assign(&mut self, r: $b) {
                self.intern -= r.intern;
            }
        }
    }
}

impl_add!(TimeDelta, TimeDelta, TimeDelta);
impl_add!(TimePoint, TimeDelta, TimePoint);
impl_add!(TimeDelta, TimePoint, TimePoint);

impl_sub!(TimeDelta, TimeDelta, TimeDelta);
impl_sub!(TimePoint, TimePoint, TimeDelta);
impl_sub!(TimePoint, TimeDelta, TimePoint);
impl_sub!(TimeDelta, TimePoint, TimePoint);

impl_add_assign!(TimeDelta, TimeDelta);
impl_add_assign!(TimePoint, TimeDelta);

impl_sub_assign!(TimeDelta, TimeDelta);
impl_sub_assign!(TimePoint, TimeDelta);

/// A time span (e.g. time in which a subtitle is shown).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TimeSpan {
    /// Start of the time span.
    pub start: TimePoint,

    /// End of the time span.
    pub end: TimePoint,
}

impl TimeSpan {
    /// Constructor of `TimeSpan`s.
    pub fn new(start: TimePoint, end: TimePoint) -> TimeSpan {
        TimeSpan {
            start: start,
            end: end,
        }
    }

    /// Get the length of the `TimeSpan` (can be negative).
    pub fn len(&self) -> TimeDelta {
        self.end - self.start
    }
}

impl Add<TimeDelta> for TimeSpan {
    type Output = TimeSpan;
    fn add(self, rhs: TimeDelta) -> TimeSpan {
        TimeSpan::new(self.start + rhs, self.end + rhs)
    }
}

impl Sub<TimeDelta> for TimeSpan {
    type Output = TimeSpan;
    fn sub(self, rhs: TimeDelta) -> TimeSpan {
        TimeSpan::new(self.start - rhs, self.end - rhs)
    }
}

impl AddAssign<TimeDelta> for TimeSpan {
    fn add_assign(&mut self, r: TimeDelta) {
        self.start += r;
        self.end += r;
    }
}

impl SubAssign<TimeDelta> for TimeSpan {
    fn sub_assign(&mut self, r: TimeDelta) {
        self.start -= r;
        self.end -= r;
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_timing_display() {
        let t = -super::Timing::from_components(12, 59, 29, 450);
        assert_eq!(t.to_string(), "-12:59:29.450".to_string());

        let t = super::Timing::from_msecs(0);
        assert_eq!(t.to_string(), "0:00:00.000".to_string());
    }
}