From a8425cfb47757e58b3b38e8a29f6d67af1dad651 Mon Sep 17 00:00:00 2001 From: Phil Date: Thu, 30 Sep 2021 09:49:00 -0700 Subject: [PATCH 1/3] Add a stopwatch to time.odin --- core/time/time.odin | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/core/time/time.odin b/core/time/time.odin index 1fcb20cf8..d1406e88e 100644 --- a/core/time/time.odin +++ b/core/time/time.odin @@ -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) } From bbccf9ddbf65d639fd3c194b2e91f2d1110496d2 Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 3 Oct 2021 15:25:41 -0700 Subject: [PATCH 2/3] Add clock overloads for Duration and Stopwatch --- core/time/time.odin | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/core/time/time.odin b/core/time/time.odin index d1406e88e..7880b8da4 100644 --- a/core/time/time.odin +++ b/core/time/time.odin @@ -135,6 +135,7 @@ duration_round :: proc(d, m: Duration) -> Duration { } return MAX_DURATION } + duration_truncate :: proc(d, m: Duration) -> Duration { return d if m <= 0 else d - d%m } @@ -148,17 +149,33 @@ year :: proc(t: Time) -> (year: int) { year, _, _, _ = _date(t, true) return } + month :: proc(t: Time) -> (month: Month) { _, month, _, _ = _date(t, true) return } + day :: proc(t: Time) -> (day: int) { _, _, day, _ = _date(t, true) return } -clock :: proc(t: Time) -> (hour, min, sec: int) { - sec = int(_time_abs(t) % SECONDS_PER_DAY) +clock :: proc { clock_from_time, clock_from_duration, clock_from_stopwatch } + +clock_from_time :: proc(t: Time) -> (hour, min, sec: int) { + return clock_from_seconds(_time_abs(t)) +} + +clock_from_duration :: proc(d: Duration) -> (hour, min, sec: int) { + return clock_from_seconds(u64(d/1e9)) +} + +clock_from_stopwatch :: proc(s: Stopwatch) -> (hour, min, sec: int) { + return clock_from_duration(duration(s)) +} + +clock_from_seconds :: proc(nsec: u64) -> (hour, min, sec: int) { + sec = int(nsec % SECONDS_PER_DAY) hour = sec / SECONDS_PER_HOUR sec -= hour * SECONDS_PER_HOUR min = sec / SECONDS_PER_MINUTE @@ -166,12 +183,10 @@ clock :: proc(t: Time) -> (hour, min, sec: int) { return } - read_cycle_counter :: proc() -> u64 { return u64(intrinsics.read_cycle_counter()) } - unix :: proc(sec: i64, nsec: i64) -> Time { sec, nsec := sec, nsec if nsec < 0 || nsec >= 1e9 { From e4ce0171834c6b4b5e35a7f26ac512698769775b Mon Sep 17 00:00:00 2001 From: Phil H Date: Thu, 28 Oct 2021 14:07:10 -0700 Subject: [PATCH 3/3] better naming conventions for stopwatch procedures + fields --- core/time/time.odin | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/time/time.odin b/core/time/time.odin index 7880b8da4..fddb09d85 100644 --- a/core/time/time.odin +++ b/core/time/time.odin @@ -46,31 +46,31 @@ Weekday :: enum int { Stopwatch :: struct { running: bool, _start_time: Tick, - _stop_time: Duration, + _accumulation: Duration, } -start :: proc(using stopwatch: ^Stopwatch) { +stopwatch_start :: proc(using stopwatch: ^Stopwatch) { if !running { _start_time = tick_now() running = true } } -stop :: proc(using stopwatch: ^Stopwatch) { +stopwatch_stop :: proc(using stopwatch: ^Stopwatch) { if running { - _stop_time += tick_diff(_start_time, tick_now()) + _accumulation += tick_diff(_start_time, tick_now()) running = false } } -reset :: proc(using stopwatch: ^Stopwatch) { - _stop_time = {} +stopwatch_reset :: proc(using stopwatch: ^Stopwatch) { + _accumulation = {} running = false } -duration :: proc(using stopwatch: Stopwatch) -> Duration { - if !running { return _stop_time } - return _stop_time + tick_diff(_start_time, tick_now()) +stopwatch_duration :: proc(using stopwatch: Stopwatch) -> Duration { + if !running { return _accumulation } + return _accumulation + tick_diff(_start_time, tick_now()) } diff :: proc(start, end: Time) -> Duration { @@ -171,7 +171,7 @@ clock_from_duration :: proc(d: Duration) -> (hour, min, sec: int) { } clock_from_stopwatch :: proc(s: Stopwatch) -> (hour, min, sec: int) { - return clock_from_duration(duration(s)) + return clock_from_duration(stopwatch_duration(s)) } clock_from_seconds :: proc(nsec: u64) -> (hour, min, sec: int) {