Commit bce120903a9c85f7bd608d3a6ba939ad86d008cd

Thomas de Grivel 2024-08-08T19:07:22

new tag type: TAG_TIME

diff --git a/lib/kc3/0.1/time.kc3 b/lib/kc3/0.1/time.kc3
index 9e2ebc5..8b31dbd 100644
--- a/lib/kc3/0.1/time.kc3
+++ b/lib/kc3/0.1/time.kc3
@@ -1,5 +1,5 @@
 defmodule Time do
 
-  defstruct [tv_sec: (Uw) 0, tv_nsec: (Uw) 0]
+  defstruct [tv_sec: (Sw) 0, tv_nsec: (Sw) 0]
 
 end
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index 3e20af6..6047c4f 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -2768,6 +2768,7 @@ sw buf_inspect_tag (s_buf *buf, const s_tag *tag)
   case TAG_STRUCT_TYPE:
     return buf_inspect_struct_type(buf, &tag->data.struct_type);
   case TAG_SYM:     return buf_inspect_sym(buf, &tag->data.sym);
+  case TAG_TIME:    return buf_inspect_time(buf, &tag->data.time);
   case TAG_TUPLE:   return buf_inspect_tuple(buf, &tag->data.tuple);
   case TAG_U8:      return buf_inspect_u8(buf, &tag->data.u8);
   case TAG_U16:     return buf_inspect_u16(buf, &tag->data.u16);
@@ -2823,6 +2824,7 @@ sw buf_inspect_tag_size (const s_tag *tag)
   case TAG_STRUCT_TYPE:
     return buf_inspect_struct_type_size(&tag->data.struct_type);
   case TAG_SYM:      return buf_inspect_sym_size(&tag->data.sym);
+  case TAG_TIME:     return buf_inspect_time_size(&tag->data.time);
   case TAG_TUPLE:    return buf_inspect_tuple_size(&tag->data.tuple);
   case TAG_U8:       return buf_inspect_u8_size(&tag->data.u8);
   case TAG_U16:      return buf_inspect_u16_size(&tag->data.u16);
@@ -2858,6 +2860,44 @@ sw buf_inspect_tag_type_size (e_tag_type type)
   return strlen(s);
 }
 
+sw buf_inspect_time (s_buf *buf, const s_time *time)
+{
+  sw r;
+  sw result = 0;
+  if ((r = buf_write_1(buf, "%Time{tv_sec: ")) < 0)
+    return r;
+  result += r;
+  if ((r = buf_inspect_sw(buf, &time->tv_sec)) < 0)
+    return r;
+  result += r;
+  if ((r = buf_write_1(buf, ", tv_nsec: ")) < 0)
+    return r;
+  result += r;
+  if ((r = buf_inspect_sw(buf, &time->tv_nsec)) < 0)
+    return r;
+  result += r;
+  if ((r = buf_write_1(buf, "}")) < 0)
+    return r;
+  result += r;
+  return result;
+}
+
+sw buf_inspect_time_size (const s_time *time)
+{
+  sw r;
+  sw result = 0;
+  result += strlen("%Time{tv_sec: ");
+  if ((r = buf_inspect_sw_size(&time->tv_sec)) < 0)
+    return r;
+  result += r;
+  result += strlen(", tv_nsec: ");
+  if ((r = buf_inspect_sw_size(&time->tv_nsec)) < 0)
+    return r;
+  result += r;
+  result += strlen("}");
+  return result;
+}
+
 sw buf_inspect_tuple (s_buf *buf, const s_tuple *tuple)
 {
   u64 i = 0;
diff --git a/libkc3/buf_inspect.h b/libkc3/buf_inspect.h
index 36bf206..f36f310 100644
--- a/libkc3/buf_inspect.h
+++ b/libkc3/buf_inspect.h
@@ -176,6 +176,8 @@ sw buf_inspect_tag (s_buf *buf, const s_tag *tag);
 sw buf_inspect_tag_size (const s_tag *tag);
 sw buf_inspect_tag_type (s_buf *buf, e_tag_type type);
 sw buf_inspect_tag_type_size (e_tag_type type);
+sw buf_inspect_time (s_buf *buf, const s_time *time);
+sw buf_inspect_time_size (const s_time *time);
 sw buf_inspect_tuple (s_buf *buf, const s_tuple *tuple);
 sw buf_inspect_tuple_size (const s_tuple *tuple);
 BUF_INSPECT_U_PROTOTYPES(8);
diff --git a/libkc3/compare.c b/libkc3/compare.c
index 644b314..2b96346 100644
--- a/libkc3/compare.c
+++ b/libkc3/compare.c
@@ -1015,6 +1015,8 @@ s8 compare_tag (const s_tag *a, const s_tag *b) {
                                                   &b->data.struct_type);
   case TAG_SYM:        return compare_str(&a->data.sym->str,
                                           &b->data.sym->str);
+  case TAG_TIME:       return compare_time(&a->data.time,
+                                           &b->data.time);
   case TAG_TUPLE:      return compare_tuple(&a->data.tuple,
                                             &b->data.tuple);
   case TAG_UNQUOTE:    return compare_unquote(&a->data.unquote,
diff --git a/libkc3/env.c b/libkc3/env.c
index 29009a5..ff0ac71 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -2528,7 +2528,8 @@ bool env_module_maybe_reload (s_env *env, const s_sym *module)
   }
   if (! r)
     return true;
-  if (! env_module_load_time(env, module, &load_time)) {
+  if (! env_module_load_time(env, module, &load_time) ||
+      ! load_time) {
     str_clean(&path);
     return env_module_load(env, module);
   }
diff --git a/libkc3/file.c b/libkc3/file.c
index 7981372..43daa89 100644
--- a/libkc3/file.c
+++ b/libkc3/file.c
@@ -10,7 +10,6 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include "assert.h"
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
@@ -19,9 +18,11 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include "alloc.h"
+#include "assert.h"
 #include "buf.h"
 #include "buf_file.h"
 #include "buf_save.h"
+#include "config.h"
 #include "env.h"
 #include "file.h"
 #include "io.h"
diff --git a/libkc3/tag.c b/libkc3/tag.c
index 54dc17d..9b714da 100644
--- a/libkc3/tag.c
+++ b/libkc3/tag.c
@@ -534,9 +534,25 @@ s_tag * tag_init_copy (s_tag *tag, const s_tag *src)
 
 s_tag * tag_init_time (s_tag *tag)
 {
-  s_timespec time;
-  clock_gettime(CLOCK_REALTIME, &time);
-  return time_to_tag(&time, tag);
+  s_time *time;
+  s_timespec timespec;
+  s_tag tmp = {0};
+  if (! tag_init_struct(&tmp, &g_sym_Time)) {
+    err_puts("tag_init_time: tag_init_struct");
+    assert(! "tag_init_time: tag_init_struct");
+    return NULL;
+  }
+  if (! struct_allocate(&tmp.data.struct_)) {
+    err_puts("tag_init_time: struct_allocate");
+    assert(! "tag_init_time: struct_allocate");
+    return NULL;
+  }
+  time = tmp.data.struct_.data;
+  clock_gettime(CLOCK_REALTIME, &timespec);
+  time->tv_sec = timespec.tv_sec;
+  time->tv_nsec = timespec.tv_nsec;
+  *tag = tmp;
+  return tag;
 }
 
 s_tag * tag_integer_reduce (s_tag *tag)
diff --git a/libkc3/types.h b/libkc3/types.h
index fac1cef..b74b14b 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -127,6 +127,7 @@ typedef enum {
   TAG_STRUCT,
   TAG_STRUCT_TYPE,
   TAG_SYM,
+  TAG_TIME,
   TAG_TUPLE,
   TAG_UNQUOTE,
   TAG_VAR,
@@ -321,8 +322,9 @@ struct tag_type_list {
 };
 
 struct time {
-  uw tv_sec;
-  uw tv_nsec;
+  s_tag *tag;
+  sw tv_sec;
+  sw tv_nsec;
 };
 
 struct tuple {
@@ -509,6 +511,7 @@ union tag_data {
   s_struct_type struct_type;
   const s_sym  *sym;
   s8            s8;
+  s_time        time;
   s16           s16;
   s32           s32;
   s64           s64;