Hash :
d4d2b2ea
Author :
Perle
Date :
2025-08-29T20:03:33
refactor: convert read_line to sw
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/* kc3
* Copyright from 2022 to 2025 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 <time.h>
#include "alloc.h"
#include "assert.h"
#include "buf.h"
#include "buf_inspect.h"
#include "str.h"
#include "sw.h"
#include "sym.h"
#include "tag.h"
#include "time.h"
s_tag * time_access (s_time *time, s_list *key, s_tag *dest)
{
s_tag *k;
assert(time);
assert(key);
assert(dest);
if (! key)
return NULL;
k = &key->tag;
if (k->type != TAG_PSYM)
return NULL;
if (k->data.psym == &g_sym_tv_sec)
return tag_init_uw(dest, time->tv_sec);
if (k->data.psym == &g_sym_tv_nsec)
return tag_init_uw(dest, time->tv_nsec);
return NULL;
}
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_str * time_diff_to_str (const s_time *time, s_str *dest)
{
char a[128] = {0};
s_buf buf = {0};
uw days;
uw hours;
uw milliseconds;
uw minutes;
uw months;
uw seconds;
uw total_seconds;
assert(time);
assert(dest);
buf_init_const(&buf, sizeof(a) - 1, a);
if (time->tv_sec < 0 || time->tv_nsec < 0 ||
time->tv_nsec > 1000 * 1000 * 1000) {
err_puts("time_diff_to_str: invalid time");
assert(! "time_diff_to_str: invalid time");
return NULL;
}
total_seconds = time->tv_sec;
milliseconds = time->tv_nsec / (1000 * 1000);
months = total_seconds / TIME_SECONDS_PER_MONTH;
total_seconds %= TIME_SECONDS_PER_MONTH;
days = total_seconds / TIME_SECONDS_PER_DAY;
total_seconds %= TIME_SECONDS_PER_DAY;
hours = total_seconds / TIME_SECONDS_PER_HOUR;
total_seconds %= TIME_SECONDS_PER_HOUR;
minutes = total_seconds / TIME_SECONDS_PER_MINUTE;
seconds = total_seconds % TIME_SECONDS_PER_MINUTE;
if (months) {
buf_inspect_uw_decimal_pad(&buf, 2, '0', months);
buf_write_1(&buf, "mo ");
}
if (days) {
buf_inspect_uw_decimal_pad(&buf, 2, '0', days);
buf_write_1(&buf, "d ");
}
if (hours) {
buf_inspect_uw_decimal_pad(&buf, 2, '0', hours);
buf_write_1(&buf, ":");
}
if (hours || minutes) {
buf_inspect_uw_decimal_pad(&buf, 2, '0', minutes);
buf_write_1(&buf, ":");
}
buf_inspect_uw_decimal_pad(&buf, 2, '0', seconds);
buf_write_1(&buf, ".");
buf_inspect_uw_decimal_pad(&buf, 3, '0', milliseconds);
if (buf_read_to_str(&buf, dest) <= 0)
return NULL;
return dest;
}
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_time * time_sub (const s_time *a, const s_time *b, s_time *dest)
{
s_time tmp = {0};
assert(a);
assert(b);
assert(dest);
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 -= 1;
tmp.tv_nsec += 1000 * 1000 * 1000;
}
*dest = tmp;
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), "%Y-%m-%d %H:%M:%S", tm)) {
err_puts("time_to_str: strftime");
assert(! "time_to_str: strftime");
return NULL;
}
return str_init_copy_1(dest, a);
}