aboutsummaryrefslogtreecommitdiff
path: root/src/time.c
blob: cbabf96e5067977bfb2a702d080df42127cfe214 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;
}