2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-04-24 07:33:25 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module time
|
|
|
|
|
2020-05-30 09:20:54 +02:00
|
|
|
pub struct StopWatchOptions {
|
|
|
|
auto_start bool = true
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// StopWatch is used to measure elapsed time.
|
2020-04-24 07:33:25 +02:00
|
|
|
pub struct StopWatch {
|
2020-04-27 22:53:26 +02:00
|
|
|
mut:
|
2020-05-30 09:20:54 +02:00
|
|
|
elapsed u64
|
2020-04-27 22:53:26 +02:00
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
start u64
|
|
|
|
end u64
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// new_stopwatch initializes a new StopWatch with the current time as start.
|
2020-05-30 09:20:54 +02:00
|
|
|
pub fn new_stopwatch(opts StopWatchOptions) StopWatch {
|
|
|
|
mut initial := u64(0)
|
|
|
|
if opts.auto_start {
|
2020-12-06 15:19:39 +01:00
|
|
|
initial = sys_mono_now()
|
|
|
|
}
|
|
|
|
return StopWatch{
|
|
|
|
elapsed: 0
|
|
|
|
start: initial
|
|
|
|
end: 0
|
2020-05-30 09:20:54 +02:00
|
|
|
}
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// start starts the stopwatch. If the timer was paused, restarts counting.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) start() {
|
2020-12-06 15:19:39 +01:00
|
|
|
t.start = sys_mono_now()
|
2020-04-24 07:33:25 +02:00
|
|
|
t.end = 0
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// restart restarts the stopwatch. If the timer was paused, restarts counting.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) restart() {
|
2020-12-06 15:19:39 +01:00
|
|
|
t.start = sys_mono_now()
|
2020-05-30 09:20:54 +02:00
|
|
|
t.end = 0
|
|
|
|
t.elapsed = 0
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// stop stops the timer, by setting the end time to the current time.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) stop() {
|
2020-12-06 15:19:39 +01:00
|
|
|
t.end = sys_mono_now()
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 15:30:54 +01:00
|
|
|
// pause resets the `start` time and adds the current elapsed time to `elapsed`.
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut t StopWatch) pause() {
|
2020-05-30 09:20:54 +02:00
|
|
|
if t.start > 0 {
|
|
|
|
if t.end == 0 {
|
2020-12-06 15:19:39 +01:00
|
|
|
t.elapsed += sys_mono_now() - t.start
|
2020-05-30 09:20:54 +02:00
|
|
|
} else {
|
|
|
|
t.elapsed += t.end - t.start
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.start = 0
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// elapsed returns the Duration since the last start call
|
|
|
|
pub fn (t StopWatch) elapsed() Duration {
|
2020-05-30 09:20:54 +02:00
|
|
|
if t.start > 0 {
|
|
|
|
if t.end == 0 {
|
2020-12-06 15:19:39 +01:00
|
|
|
return Duration(i64(sys_mono_now() - t.start + t.elapsed))
|
2020-05-30 09:20:54 +02:00
|
|
|
} else {
|
2020-11-02 01:17:35 +01:00
|
|
|
return Duration(i64(t.end - t.start + t.elapsed))
|
2020-05-30 09:20:54 +02:00
|
|
|
}
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|
2020-11-02 01:17:35 +01:00
|
|
|
return Duration(i64(t.elapsed))
|
2020-04-24 07:33:25 +02:00
|
|
|
}
|