Hash :
d3f69ed5
Author :
Thomas de Grivel
Date :
2024-11-08T20:04:29
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/* 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 "str.h"
#include "sw.h"
#include "sym.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_cast (s_time *time, const s_sym * const *type,
const s_tag *src)
{
assert(time);
assert(type);
assert(*type);
assert(src);
(void) type;
if (src->type == TAG_TIME)
return time_init_copy(time, &src->data.time);
err_puts("time_init_cast: cannot cast to Time");
assert(! "time_init_cast: cannot cast to Time");
return NULL;
}
s_time * time_init_copy (s_time *time, const s_time *src)
{
s_time tmp = {0};
tmp.tv_sec = src->tv_sec;
tmp.tv_nsec = src->tv_nsec;
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;
}
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_time * time_init_str (s_time *time, const s_str *src)
{
struct tm t = {0};
s_time tmp = {0};
sscanf(src->ptr.pchar, "%d-%d-%d %d:%d:%d",
&t.tm_year, &t.tm_mon, &t.tm_mday,
&t.tm_hour, &t.tm_min, &t.tm_sec);
t.tm_year -= 1900;
t.tm_mon -= 1;
tmp.tv_sec = timegm(&t);
*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_str * time_to_str (const s_time *time, s_str *dest)
{
char a[64] = {0};
time_t t;
const struct tm *tm;
t = time->tv_sec;
if (! (tm = gmtime(&t))) {
err_puts("time_to_str: gmtime");
assert(! "time_to_str: gmtime");
return NULL;
}
if (! strftime(a, sizeof(a), "%F %T", tm)) {
err_puts("time_to_str: strftime");
assert(! "time_to_str: strftime");
return NULL;
}
return str_init_copy_1(dest, a);
}