From d267735d99449cf2836842bd4499f15d536df0d2 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sat, 10 Aug 2024 12:35:58 +0200 Subject: [PATCH] Fixed time.precise_clock --- core/time/time.odin | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/core/time/time.odin b/core/time/time.odin index e4ec67be3..d02a16c1a 100644 --- a/core/time/time.odin +++ b/core/time/time.odin @@ -344,11 +344,32 @@ Obtain the time components from a time, a duration or a stopwatch's total. */ clock :: proc { clock_from_time, clock_from_duration, clock_from_stopwatch } +/* +Obtain the time components from a time, a duration or a stopwatch's total, including nanoseconds. +*/ +precise_clock :: proc { precise_clock_from_time, precise_clock_from_duration, precise_clock_from_stopwatch } + /* Obtain the time components from a time. */ clock_from_time :: proc "contextless" (t: Time) -> (hour, min, sec: int) { - return clock_from_seconds(_time_abs(t)) + hour, min, sec, _ = precise_clock_from_time(t) + return +} + +/* +Obtain the time components from a time, including nanoseconds. +*/ +precise_clock_from_time :: proc "contextless" (t: Time) -> (hour, min, sec, nanos: int) { + // Time in nanoseconds since 1-1-1970 00:00 + sec, nanos = int(t._nsec) / 1e9, int(t._nsec) % 1e9 + sec += int(INTERNAL_TO_ABSOLUTE) + sec = sec % SECONDS_PER_DAY + hour = sec / SECONDS_PER_HOUR + sec -= hour * SECONDS_PER_HOUR + min = sec / SECONDS_PER_MINUTE + sec -= min * SECONDS_PER_MINUTE + return } /* @@ -358,6 +379,13 @@ clock_from_duration :: proc "contextless" (d: Duration) -> (hour, min, sec: int) return clock_from_seconds(u64(d/1e9)) } +/* +Obtain the time components from a duration, including nanoseconds. +*/ +precise_clock_from_duration :: proc "contextless" (d: Duration) -> (hour, min, sec, nanos: int) { + return precise_clock_from_time({_nsec=i64(d)}) +} + /* Obtain the time components from a stopwatch's total. */ @@ -365,11 +393,18 @@ clock_from_stopwatch :: proc "contextless" (s: Stopwatch) -> (hour, min, sec: in return clock_from_duration(stopwatch_duration(s)) } +/* +Obtain the time components from a stopwatch's total, including nanoseconds +*/ +precise_clock_from_stopwatch :: proc "contextless" (s: Stopwatch) -> (hour, min, sec, nanos: int) { + return precise_clock_from_duration(stopwatch_duration(s)) +} + /* Obtain the time components from the number of seconds. */ -clock_from_seconds :: proc "contextless" (nsec: u64) -> (hour, min, sec: int) { - sec = int(nsec % SECONDS_PER_DAY) +clock_from_seconds :: proc "contextless" (in_sec: u64) -> (hour, min, sec: int) { + sec = int(in_sec % SECONDS_PER_DAY) hour = sec / SECONDS_PER_HOUR sec -= hour * SECONDS_PER_HOUR min = sec / SECONDS_PER_MINUTE