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
//! Helper module to parse, print, manipulate timestamps.
//!
//! SSA has a timestamp representation of `00:00:00.00`
//!
//! VTT has a timestamp representation of `00:00:00.000`
//!
//! SRT has a timestamp representation of `00:00:00,000`

use serde::Deserialize;
use serde::Serialize;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Sub;
use std::result::Result;
use std::str::FromStr;
use std::{fmt, ops::Div};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Time {
    pub h: u32,
    pub m: u32,
    pub s: u32,
    pub ms: u32,
    pub frames: u32,
    pub fps: f32,
}
impl Eq for Time {}
impl Default for Time {
    fn default() -> Self {
        let mut t = Time {
            h: 0,
            m: 0,
            s: 0,
            ms: 0,
            frames: 0,
            fps: 0.0,
        };
        t.derive_frames();
        t
    }
}

impl FromStr for Time {
    type Err = std::num::ParseIntError;
    fn from_str(str: &str) -> Result<Self, <Time as FromStr>::Err> {
        let mut t = Time::default();
        let splits = str.split(':').collect::<Vec<&str>>();
        match splits.len() {
            2 => {
                t.h = 0;
                t.m = splits.first().unwrap_or(&"0").to_string().parse::<u32>()?;
                let sms = splits
                    .get(1)
                    .unwrap_or(&"0.0")
                    .replace(',', ".")
                    .trim()
                    .parse::<f32>()
                    .unwrap();
                let fms = format!("{sms:.3}");
                let msf = fms.split('.').collect::<Vec<&str>>();
                t.s = msf.first().unwrap_or(&"0").to_string().parse::<u32>()?;
                t.ms = msf.get(1).unwrap_or(&"0").to_string().parse::<u32>()?;
            }
            3 => {
                t.h = splits.first().unwrap_or(&"0").to_string().parse::<u32>()?;
                t.m = splits.get(1).unwrap_or(&"0").to_string().parse::<u32>()?;
                let sms = splits
                    .get(2)
                    .unwrap_or(&"0.0")
                    .replace(',', ".")
                    .trim()
                    .parse::<f32>()
                    .unwrap();
                let fms = format!("{sms:.3}");
                let msf = fms.split('.').collect::<Vec<&str>>();
                t.s = msf.first().unwrap_or(&"0").to_string().parse::<u32>()?;
                t.ms = msf.get(1).unwrap_or(&"0").to_string().parse::<u32>()?;
            }
            _ => {
                panic!("Bad Time")
            }
        }

        Ok(t)
    }
}
pub fn frames_to_ms(frames: u32, fps: f32) -> u32 {
    if frames == 0 || fps == 0.0 {
        0
    } else {
        (((frames as f32) * (10000.0)) / fps).round() as u32 / 10
    }
}
pub fn ms_to_frames(ms: u32, fps: f32) -> u32 {
    if fps == 0.0 {
        0
    } else {
        ((ms as f32) * fps / 1000.0).round() as u32
    }
}
pub fn ms_to_timestring(ms: u32) -> String {
    let hms = ms.div(3600000);
    let mms = (ms - hms * 3600000).div(60000);
    let sms = (ms - mms * 60000 - hms * 3600000).div(1000);
    let msms = &format!(
        "0.{:0>3}",
        ((ms - mms * 60000 - hms * 3600000 - sms * 1000) as f32)
    );
    let mmmms = msms
        .split('.')
        .collect::<Vec<&str>>()
        .get(1)
        .or(Some(&"0"))
        .expect("Should be good")
        .to_string();
    format!("{hms:0>2}")
        + ":"
        + &format!("{mms:0>2}")
        + ":"
        + &format!("{sms:0>2}")
        + "."
        + &format!("{mmmms:0>3}")
}

impl std::error::Error for Time {}
impl Time {
    pub fn total_ms(&self) -> u32 {
        self.h * 3600000 + self.m * 60000 + self.s * 1000 + self.ms
    }
    pub fn update_from_fps_frames(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        let t = Time::from_str(&ms_to_timestring(frames_to_ms(self.frames, self.fps)))?;
        self.h = t.h;
        self.m = t.m;
        self.s = t.s;
        self.ms = t.ms;
        Ok(())
    }
    pub fn derive_frames(&mut self) {
        self.frames = ms_to_frames(self.total_ms(), self.fps);
    }
    pub fn set_fps(&mut self, fps: f32) {
        self.fps = fps;
        self.derive_frames();
    }
    pub fn update_from_ms(&mut self, ms: u32) -> Result<(), Box<dyn std::error::Error>> {
        let t = Time::from_str(&ms_to_timestring(ms))?;
        self.h = t.h;
        self.m = t.m;
        self.s = t.s;
        self.ms = t.ms;
        self.derive_frames();
        Ok(())
    }

    // Adds <u32>ms to `self` and updates.
    pub fn add_ms(&mut self, ms: u32) -> Result<(), Box<dyn std::error::Error>> {
        self.update_from_ms(self.total_ms() + ms)?;
        Ok(())
    }
    // Subtracts <u32>ms from `self` and updates. Panics if total ms < 0
    pub fn sub_ms(&mut self, ms: u32) -> Result<(), &mut Time> {
        if ms > self.total_ms() {
            self.update_from_ms(self.total_ms() - self.total_ms())
                .unwrap();
            Err(self)
        } else {
            self.update_from_ms(self.total_ms() - ms).unwrap();
            Ok(())
        }
    }
    pub fn to_ass_string(self) -> String {
        format!(
            "{:0>1}:{:0>2}:{:0>2}.{:0>2}",
            self.h,
            self.m,
            self.s,
            self.ms / 10
        )
    }
    pub fn to_srt_string(self) -> String {
        format!(
            "{:0>2}:{:0>2}:{:0>2},{:0>3}",
            self.h, self.m, self.s, self.ms
        )
    }
}
// Add <u32>ms to a `Time` struct
impl Add<u32> for Time {
    type Output = Time;
    fn add(mut self, other: u32) -> Time {
        self.add_ms(other).unwrap();
        self
    }
}
impl AddAssign<u32> for Time {
    fn add_assign(&mut self, other: u32) {
        self.add_ms(other).unwrap();
    }
}
impl AddAssign<i32> for Time {
    fn add_assign(&mut self, other: i32) {
        self.add_ms(other.try_into().unwrap()).unwrap();
    }
}
// Subtracts <u32>ms to a `Time` struct
impl Sub<u32> for Time {
    type Output = Self;
    fn sub(mut self, other: u32) -> Self {
        self.sub_ms(other).unwrap_or_default();
        self
    }
} // Subtracts <i32>ms to a `Time` struct
impl Sub<i32> for Time {
    type Output = Self;
    fn sub(mut self, other: i32) -> Self {
        self.sub_ms(other.try_into().unwrap()).unwrap_or_default();
        self
    }
}
impl Sub<i32> for &mut Time {
    type Output = Self;
    fn sub(self, other: i32) -> Self {
        self.sub_ms(other.try_into().unwrap()).unwrap_or_default();
        self
    }
}
impl Sub<u32> for &mut Time {
    type Output = Self;
    fn sub(self, other: u32) -> Self {
        self.sub_ms(other).unwrap_or_default();
        self
    }
}
// Add <i32>ms to a `Time` struct
impl Add<i32> for Time {
    type Output = Self;
    fn add(mut self, other: i32) -> Self {
        self.add_ms(other.try_into().unwrap()).unwrap();
        self
    }
}
// Add <i32>ms to a `Time` struct
impl Add<i32> for &mut Time {
    type Output = Self;
    fn add(self, other: i32) -> Self {
        self.add_ms(other as u32).unwrap();
        self
    }
}
// Displays the time as `hh:mm:ss.mss`
impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:0>2}:{:0>2}:{:0>2}.{:0>3}",
            self.h, self.m, self.s, self.ms
        )
    }
}