pub fn parse_float_time(s: &str) -> Result<Duration, String>
Expand description

Precisely parses an duration from a string of the form ‘{secs}.{sub-secs}’

Examples found in repository?
src/configuration/time.rs (line 161)
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
    fn parse(s: &str) -> Result<Duration, String> {
        parse_float_time(s)
    }
}
impl OutputTimeRepresentation for RelativeFloat {}
impl TimeMode for RelativeFloat {}

/// Time represented as the unsigned number in nanoseconds as the offset to the preceding event.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct OffsetNanos {
    current: Time,
    last_time: Time,
}

impl TimeRepresentation for OffsetNanos {
    type InnerTime = u64;

    fn convert_from(&mut self, raw: Self::InnerTime) -> Time {
        self.last_time = self.current;
        self.current += Duration::from_nanos(raw);
        self.current
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        ts.sub(self.last_time).as_nanos() as u64
    }

    fn to_string(&self, ts: Self::InnerTime) -> String {
        ts.to_string()
    }

    fn parse(s: &'_ str) -> Result<u64, String> {
        u64::from_str(s).map_err(|e| e.to_string())
    }
}
impl TimeMode for OffsetNanos {}

/// Time represented as a positive real number representing seconds and sub-seconds as the offset to the preceding event.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct OffsetFloat {
    current: Time,
    last_time: Time,
}

impl TimeRepresentation for OffsetFloat {
    type InnerTime = Duration;

    fn convert_from(&mut self, ts: Duration) -> Time {
        self.last_time = self.current;
        self.current += ts;
        self.current
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        ts - self.last_time
    }

    fn to_string(&self, ts: Time) -> String {
        let dur = self.convert_into(ts);
        format! {"{}.{:09}", dur.as_secs(), dur.subsec_nanos()}
    }

    fn parse(s: &str) -> Result<Duration, String> {
        parse_float_time(s)
    }
}

impl TimeMode for OffsetFloat {}

/// Time represented as wall clock time given as a positive real number representing seconds and sub-seconds since the start of the Unix Epoch.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default)]
pub struct AbsoluteFloat {}

impl TimeRepresentation for AbsoluteFloat {
    type InnerTime = Duration;

    fn convert_from(&mut self, ts: Duration) -> Time {
        let current = SystemTime::UNIX_EPOCH + ts;
        let st_read = *START_TIME.read().unwrap();
        if let Some(st) = st_read {
            current.duration_since(st).expect("Time did not behave monotonically!")
        } else {
            *START_TIME.write().unwrap() = Some(current);
            Duration::ZERO
        }
    }

    fn convert_into(&self, ts: Time) -> Self::InnerTime {
        let ts = START_TIME.read().unwrap().unwrap() + ts;
        ts.duration_since(SystemTime::UNIX_EPOCH)
            .expect("Time did not behave monotonically!")
    }

    fn to_string(&self, ts: Time) -> String {
        let dur = self.convert_into(ts);
        format! {"{}.{:09}", dur.as_secs(), dur.subsec_nanos()}
    }

    fn parse(s: &str) -> Result<Duration, String> {
        parse_float_time(s)
    }