2020-04-24 07:33:25 +02:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module time
|
|
|
|
|
|
|
|
pub struct StopWatch {
|
2020-04-27 22:53:26 +02:00
|
|
|
mut:
|
2020-04-24 07:33:25 +02:00
|
|
|
pause_time u64
|
2020-04-27 22:53:26 +02:00
|
|
|
pub mut:
|
2020-04-24 07:33:25 +02:00
|
|
|
start u64
|
|
|
|
end u64
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_stopwatch() StopWatch {
|
2020-05-18 15:51:36 +02:00
|
|
|
return StopWatch{pause_time: 0, start: time.sys_mono_now(), end: 0}
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// start Starts the timer. If the timer was paused, restarts counting.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) start() {
|
2020-04-24 07:33:25 +02:00
|
|
|
if t.pause_time == 0 {
|
|
|
|
t.start = time.sys_mono_now()
|
|
|
|
} else {
|
|
|
|
t.start += time.sys_mono_now() - t.pause_time
|
|
|
|
}
|
|
|
|
t.end = 0
|
|
|
|
t.pause_time = 0
|
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) restart() {
|
2020-04-24 07:33:25 +02:00
|
|
|
t.end = 0
|
|
|
|
t.pause_time = 0
|
|
|
|
t.start = time.sys_mono_now()
|
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) stop() {
|
2020-04-24 07:33:25 +02:00
|
|
|
t.end = time.sys_mono_now()
|
|
|
|
t.pause_time = 0
|
|
|
|
}
|
|
|
|
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) pause() {
|
2020-04-24 07:33:25 +02:00
|
|
|
t.pause_time = time.sys_mono_now()
|
|
|
|
t.end = t.pause_time // so elapsed keeps track of actual running time
|
|
|
|
}
|
|
|
|
|
|
|
|
// elapsed returns the Duration since the last start call
|
|
|
|
pub fn (t StopWatch) elapsed() Duration {
|
|
|
|
if t.end == 0 {
|
|
|
|
return Duration(time.sys_mono_now() - t.start)
|
|
|
|
} else {
|
|
|
|
return Duration(t.end - t.start)
|
|
|
|
}
|
|
|
|
}
|