-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.rs
51 lines (45 loc) · 1.12 KB
/
timer.rs
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
use std::time::{Duration, Instant};
use std::ops::Sub;
#[derive(Debug, Clone, PartialEq)]
pub struct Timer{
duration: Duration,
start_time: Instant,
}
impl Timer{
pub fn new(duration_millis: u64)-> Timer{
Timer{
duration: Duration::from_millis(duration_millis),
start_time: Instant::now(),
}
}
pub fn new_sec(duration_sec: u64)-> Timer{
Timer{
duration: Duration::from_secs(duration_sec),
start_time: Instant::now(),
}
}
pub fn advance_by(&mut self, duration: Duration){
self.start_time = self.start_time.sub(duration);
}
pub fn finished(&self) -> bool{
let current_time = Instant::now();
let elapsed = current_time - self.start_time;
elapsed >= self.duration
}
#[allow(dead_code)]
pub fn set_duration(&mut self, duration: u64){
self.duration = Duration::from_secs(duration);
}
pub fn restart(&mut self){
self.start_time = Instant::now();
}
pub fn value(&self)-> f32{
let current_time = Instant::now();
let elapsed = current_time - self.start_time;
if elapsed < self.duration{
1.0 * (100.0 / self.duration.as_millis() as f32 * elapsed.as_millis() as f32) / 100.0
}else{
1.0
}
}
}