aboutsummaryrefslogtreecommitdiff
path: root/src/std/time.c
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-04-08 13:14:27 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-04-08 13:15:01 +0200
commitcf534d762754860ed5b714974f99acd282d48b3b (patch)
treebf22341cedfb50ee7bcd4e15f85add2329783f31 /src/std/time.c
parent630fcb1846aed16b9ab62bcf700f668883f4521f (diff)
Restructure into src/{std,json}/
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'src/std/time.c')
-rw-r--r--src/std/time.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/std/time.c b/src/std/time.c
new file mode 100644
index 0000000..331f1d5
--- /dev/null
+++ b/src/std/time.c
@@ -0,0 +1,48 @@
+#include <time.h>
+
+#include "assert.h"
+#include "time.h"
+#include "types.h"
+
+Time Time_get() {
+ struct timespec system_time;
+ assert(clock_gettime(CLOCK_MONOTONIC, &system_time) == 0, "clock_gettime failed");
+ return (Time){system_time.tv_sec, system_time.tv_nsec};
+}
+
+f32 Time_ms(Time time) {
+ return time.s * 1e3 + time.ns / 1e6;
+}
+
+Duration Time_diff(Time end, Time start) {
+ Duration d;
+ d.s = end.s - start.s;
+ d.ns = end.ns - start.ns;
+ if (d.ns >= (u64)1e9) {
+ d.s += d.ns / (u64)1e9;
+ d.ns = d.ns % (u64)1e9;
+ }
+ return d;
+}
+
+f32 Duration_ms(Duration d) {
+ return d.s * 1e3 + d.ns / 1e6;
+}
+
+Duration Duration_from_ms(f32 ms) {
+ Duration d;
+ d.s = (u64)(ms / 1e3);
+ d.ns = (u64)((ms - d.s * 1e3) * 1e6);
+ return d;
+}
+
+Duration Duration_diff(Duration a, Duration b) {
+ Duration d;
+ d.s = a.s - b.s;
+ d.ns = a.ns - b.ns;
+ if (d.ns >= (u64)1e9) {
+ d.s += d.ns / (u64)1e9;
+ d.ns = d.ns % (u64)1e9;
+ }
+ return d;
+}