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

use std::time::{Instant, Duration};
use std::default::Default;
use std::fmt;
use std::ops::{Add, Sub};
use rust_decimal::Decimal;

#[derive(Default, Clone, Copy)]
pub struct TimeSpan {
    nanos: u128,
}

impl From<Duration> for TimeSpan {
    fn from(d:Duration) -> Self {
        Self{
            nanos: d.as_nanos(),
        }
    }
}

impl Into<Duration> for TimeSpan{
    fn into(self) -> Duration{
        Duration::from_nanos(self.nanos as u64)
    } 
}

impl Add<TimeSpan> for TimeSpan{
    type Output = TimeSpan;
    fn add(self, t: TimeSpan) -> TimeSpan {
        TimeSpan{
            nanos: self.nanos + t.nanos
        }
    }
}

impl Add<Duration> for TimeSpan{
    type Output = TimeSpan;
    fn add(self, d: Duration) -> TimeSpan {
        TimeSpan{
            nanos: self.nanos + d.as_nanos()
        }
    }
}


impl Sub<TimeSpan> for TimeSpan{
    type Output = TimeSpan;
    fn sub(self, t: TimeSpan) -> TimeSpan {
        TimeSpan{
            nanos: self.nanos - t.nanos
        }
    }
}

impl Sub<Duration> for TimeSpan{
    type Output = TimeSpan;
    fn sub(self, d: Duration) -> TimeSpan {
        TimeSpan{
            nanos: self.nanos - d.as_nanos()
        }
    }
}




impl TimeSpan {
    //const NANOS_PER_MICRO: u32 = 1_000;
    const NANOS_PER_MILLI: u128 = 1_000_000;
    const NANOS_PER_SEC: u128 = 1_000_000_000;
    const NANOS_PER_MINUTE: u128 = Self::NANOS_PER_SEC * 60;
    const NANOS_PER_HOUR: u128 = Self::NANOS_PER_MINUTE * 60;
    const NANOS_PER_DAY: u128 = Self::NANOS_PER_HOUR * 24;


  
    pub fn total_days(&self) -> Decimal{
        Decimal::new(self.nanos as i64, 0) * (Decimal::new(1, 0) / Decimal::new(Self::NANOS_PER_DAY as i64, 0))
    }

    pub fn total_hours(&self) -> Decimal{
        Decimal::new(self.nanos as i64, 0) *  (Decimal::new(1, 0) / Decimal::new(Self::NANOS_PER_HOUR as i64, 0))
    }

    pub fn total_minutes(&self) -> Decimal{
        Decimal::new(self.nanos as i64, 0) *  (Decimal::new(1, 0) / Decimal::new(Self::NANOS_PER_MINUTE as i64, 0))
    }

    pub fn total_seconds(&self) -> Decimal{
        Decimal::new(self.nanos as i64, 0) *  (Decimal::new(1, 0) / Decimal::new(Self::NANOS_PER_SEC as i64, 0))
    }

    pub fn total_ms(&self) -> Decimal {
        Decimal::new(self.nanos as i64, 0) *  (Decimal::new(1, 0) / Decimal::new(Self::NANOS_PER_MILLI as i64, 0))
    }
    pub fn total_nano(&self) -> u128 {
        self.nanos
    }

    pub fn days(&self) -> u32 {
        (self.nanos / Self::NANOS_PER_DAY) as u32
    }
    pub fn hours(&self) -> u32 {
        ((self.nanos / Self::NANOS_PER_HOUR) % 24) as u32
    }
    pub fn minutes(&self) -> u32 {
        ((self.nanos / Self::NANOS_PER_MINUTE) % 60) as u32
    }
    pub fn seconds(&self) -> u32 {
        ((self.nanos / Self::NANOS_PER_SEC) % 60) as u32
    }

    pub fn ms(&self) -> u32 {
        ((self.nanos / Self::NANOS_PER_MILLI) % 1000) as u32
    }

    pub fn nanos(&self) -> u128 {
        self.nanos
    }
    
}

#[derive(Default, Clone, Copy)]
pub struct Profiler {
    start_time: Option<Instant>,
    end_time: Option<Instant>,
    elapsed_time: TimeSpan,
}

impl fmt::Display for Profiler {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let elapsed = self.elapsed();
		return write!(f, "{}ms", elapsed.total_minutes());
	}
}

impl Profiler {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn start_new() -> Self{
        let mut inst = Self::default();
        inst.start();
        inst
    }

    pub fn start(&mut self) {
        if self.start_time.is_none(){
            self.start_time = Some(Instant::now());
        }
    }

    pub fn stop(&mut self) {
        if self.end_time.is_none(){
            self.end_time = Some(Instant::now());
        }

        if let (Some(start), Some(end)) = (self.start_time, self.end_time){
            self.elapsed_time = (end - start).into();
        }else{
            self.elapsed_time = TimeSpan::default();
        }
    }

    pub fn clear(&mut self) {
        self.start_time = None;
        self.end_time = None
    }

    pub fn since_started(&self) -> TimeSpan{
        let current_inst = Instant::now();
        if let Some(started) = self.start_time{
            (current_inst - started).into()
        }else{
            Duration::from_secs(0).into()
        }
    }

    pub fn elapsed(&self) -> TimeSpan {
        self.elapsed_time
    }

}