Tag
Hash :
61ae2f2b
Author :
Thomas de Grivel
Date :
2024-08-09T13:45:13
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <stdlib.h>
#include "alloc.h"
#include "assert.h"
#include "tag.h"
#include "time.h"
s_time * time_allocate (s_time *time)
{
time->tag = alloc(2 * sizeof(s_tag));
if (! time->tag)
return NULL;
return time;
}
void time_clean (s_time *time)
{
if (time->tag) {
tag_clean(time->tag);
tag_clean(time->tag + 1);
free(time->tag);
}
}
s_time * time_init (s_time *time)
{
s_time tmp = {0};
*time = tmp;
return time;
}
s_time * time_init_add (s_time *time, const s_time *a, const s_time *b)
{
s_time tmp = {0};
assert(time);
assert(a);
assert(b);
assert(! a->tag);
assert(! b->tag);
tmp.tv_sec = a->tv_sec + b->tv_sec;
tmp.tv_nsec = a->tv_nsec + b->tv_nsec;
while (tmp.tv_nsec < 0) {
tmp.tv_sec--;
tmp.tv_nsec += 1000000000;
}
while (tmp.tv_nsec > 1000000000) {
tmp.tv_sec++;
tmp.tv_nsec -= 1000000000;
}
*time = tmp;
return time;
}
s_time * time_init_copy (s_time *time, const s_time *src)
{
s_time tmp = {0};
if (src->tag) {
if (! time_allocate(&tmp))
return NULL;
if (! tag_init_copy(tmp.tag, src->tag)) {
free(tmp.tag);
return NULL;
}
if (! tag_init_copy(tmp.tag + 1, src->tag + 1)) {
tag_clean(tmp.tag);
free(tmp.tag);
return NULL;
}
*time = tmp;
return time;
}
else {
tmp.tv_sec = src->tv_sec;
tmp.tv_nsec = src->tv_nsec;
}
*time = tmp;
return time;
}
s_time * time_init_now (s_time *time)
{
s_timespec timespec;
s_time tmp = {0};
clock_gettime(CLOCK_REALTIME, ×pec);
tmp.tv_sec = timespec.tv_sec;
tmp.tv_nsec = timespec.tv_nsec;
*time = tmp;
return time;
}
s_timespec * time_sub (const s_timespec *a, const s_timespec *b, s_timespec *dest)
{
if ((a->tv_nsec - b->tv_nsec) < 0) {
dest->tv_sec = a->tv_sec - b->tv_sec - 1;
dest->tv_nsec = 1000000000 + a->tv_nsec - b->tv_nsec;
} else {
dest->tv_sec = a->tv_sec - b->tv_sec;
dest->tv_nsec = a->tv_nsec - b->tv_nsec;
}
return dest;
}
f64 * time_to_f64 (const s_timespec *time, f64 *dest)
{
*dest = (f64) time->tv_sec + (f64) time->tv_nsec * 0.000000001;
return dest;
}
s_tag * time_to_tag (const s_timespec *time, s_tag *dest)
{
s_tag tmp = {0};
assert(time);
if (! tag_init_tuple(&tmp, 2))
return NULL;
tag_init_s64(&tmp.data.tuple.tag[0], time->tv_sec);
tag_init_s64(&tmp.data.tuple.tag[1], time->tv_nsec);
*dest = tmp;
return dest;
}