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
//! Extensions and utilities for working with time.

/// An extension for the `std::time::Duration` type providing some simple methods for easy access
/// to an `f64` representation of seconds, ms, mins, hrs, and other units of time.
///
/// While these measurements make it easier to work with sketches and artworks, it's worth noting
/// that resolution may be lost, especially at high values.
pub trait DurationF64 {
    /// A simple way of retrieving the duration in seconds.
    fn secs(&self) -> f64;

    /// A simple way of retrieving the duration in milliseconds.
    ///
    /// By default, this is implemented as `self.secs() * 1_000.0`.
    fn ms(&self) -> f64 {
        self.secs() * 1_000.0
    }

    /// A simple way of retrieving the duration as minutes.
    fn mins(&self) -> f64 {
        self.secs() / 60.0
    }

    /// A simple way of retrieving the duration as hrs.
    fn hrs(&self) -> f64 {
        self.secs() / 3_600.0
    }

    /// A simple way of retrieving the duration as days.
    fn days(&self) -> f64 {
        self.secs() / 86_400.0
    }

    /// A simple way of retrieving the duration as weeks.
    fn weeks(&self) -> f64 {
        self.secs() / 604_800.0
    }
}

impl DurationF64 for std::time::Duration {
    fn secs(&self) -> f64 {
        self.as_secs() as f64 + self.subsec_nanos() as f64 * 1e-9
    }
}