mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-19 04:50:29 +00:00
Add a stopwatch to time.odin
This commit is contained in:
@@ -43,6 +43,36 @@ Weekday :: enum int {
|
||||
Saturday,
|
||||
}
|
||||
|
||||
Stopwatch :: struct {
|
||||
running: bool,
|
||||
_start_time: Tick,
|
||||
_stop_time: Duration,
|
||||
}
|
||||
|
||||
start :: proc(using stopwatch: ^Stopwatch) {
|
||||
if !running {
|
||||
_start_time = tick_now()
|
||||
running = true
|
||||
}
|
||||
}
|
||||
|
||||
stop :: proc(using stopwatch: ^Stopwatch) {
|
||||
if running {
|
||||
_stop_time += tick_diff(_start_time, tick_now())
|
||||
running = false
|
||||
}
|
||||
}
|
||||
|
||||
reset :: proc(using stopwatch: ^Stopwatch) {
|
||||
_stop_time = {}
|
||||
running = false
|
||||
}
|
||||
|
||||
duration :: proc(using stopwatch: Stopwatch) -> Duration {
|
||||
if !running { return _stop_time }
|
||||
return _stop_time + tick_diff(_start_time, tick_now())
|
||||
}
|
||||
|
||||
diff :: proc(start, end: Time) -> Duration {
|
||||
d := end._nsec - start._nsec
|
||||
return Duration(d)
|
||||
@@ -52,7 +82,6 @@ since :: proc(start: Time) -> Duration {
|
||||
return diff(start, now())
|
||||
}
|
||||
|
||||
|
||||
duration_nanoseconds :: proc(d: Duration) -> i64 {
|
||||
return i64(d)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user