aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/time.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/time.c b/src/time.c
new file mode 100644
index 0000000..cbabf96
--- /dev/null
+++ b/src/time.c
@@ -0,0 +1,48 @@
+#include <time.h>
+
+#include "../include/assert.h"
+#include "../include/time.h"
+#include "../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;
+}