Commit acdd3bec6d0d281b0b3479a7ec10b4661bfdd71d

Thomas de Grivel 2024-09-16T03:34:45

strftime

diff --git a/.ikc3_history b/.ikc3_history
index 6ced118..2b0ec71 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,14 +1,3 @@
-? <- 1
-a = ? <- 1
-a
-a = ? <- 1 ; b = a
-b
-a
-a = ? <- 1 ; 2
-a
-a = ? <- 1 ; 2
-a
-(a = ? <- 1) ; 2
 a
 a = ? <- 1 ; 2
 a
@@ -97,3 +86,14 @@ double(4)
 double(21)
 List.map([1, 2, 3, 4], double)
 List.reverse([1, 2, 3, 4])
+Time.now
+Time.now()
+Str.ftime(Time.now(), "%a")
+Str.ftim
+Str.ftime
+Str.ftime(Time.now(), "%a")
+Str.ftime(Time.now(), "%a, %d")
+Str.ftime(Time.now(), "%a, %G")
+Str.ftime(Time.now(), "%a, %m %d %G")
+Str.ftime(Time.now(), "%a, %b %d %G")
+Str.ftime(Time.now(), "%a, %b %d %G %T %Z")
diff --git a/lib/kc3/0.1/str.facts b/lib/kc3/0.1/str.facts
index 404d311..f207fdb 100644
--- a/lib/kc3/0.1/str.facts
+++ b/lib/kc3/0.1/str.facts
@@ -26,3 +26,6 @@ replace {Str.has_str?, :symbol_value,
 add {Str, :symbol, Str.subst}
 replace {Str.subst, :symbol_value,
          cfn Str "str_init_subst" (Result, Str, Str, Str)}
+add {Str, :symbol, Str.ftime}
+replace {Str.ftime, :symbol_value,
+         cfn Str "str_init_ftime" (Result, Time, Str)}
diff --git a/libkc3/str.c b/libkc3/str.c
index 9606d9e..9d87e22 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -546,6 +546,32 @@ s_str * str_init_f (s_str *str, const char *fmt, ...)
 }
 
 DEF_STR_INIT_STRUCT(fn)
+
+s_str * str_init_ftime (s_str *str, s_time *time, const s_str *format)
+{
+  char *buf;
+  uw size;
+  time_t t;
+  s_str tmp;
+  const struct tm *utc = NULL;
+  t = time->tv_sec;
+  if (! (utc = gmtime(&t)))
+    return NULL;
+  size = format->size * 32;
+  if (! (buf = alloc(size)))
+    return NULL;
+  if (! strftime(buf, size - 1, format->ptr.pchar, utc))
+    goto clean;
+  if (! str_init_1_alloc(&tmp, buf))
+    goto clean;
+  free(buf);
+  *str = tmp;
+  return str;
+ clean:
+  free(buf);
+  return NULL;
+}
+
 DEF_STR_INIT_PTR(list, const s_list * const *)
 DEF_STR_INIT_STRUCT(map)
 DEF_STR_INIT_PTR(ptr, const u_ptr_w *)
diff --git a/libkc3/str.h b/libkc3/str.h
index 2b7e707..ca99123 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -55,6 +55,7 @@ s_str * str_init_copy_1 (s_str *str, const char *p);
 s_str * str_init_empty (s_str *str);
 s_str * str_init_f (s_str *str, const char *fmt, ...);
 PROTOTYPE_STR_INIT_STRUCT(fn);
+s_str * str_init_ftime (s_str *str, s_time *time, const s_str *format);
 PROTOTYPE_STR_INIT(list, const s_list * const *);
 PROTOTYPE_STR_INIT_STRUCT(map);
 PROTOTYPE_STR_INIT(ptr, const u_ptr_w *);
diff --git a/test/ikc3/time.kc3 b/test/ikc3/time.kc3
new file mode 100644
index 0000000..b2d2f55
--- /dev/null
+++ b/test/ikc3/time.kc3
@@ -0,0 +1,4 @@
+quote %Time{}
+%Time{}
+quote Str.ftime(%Time{}, "%a, %b %d %G %T %Z")
+Str.ftime(%Time{}, "%a, %b %d %G %T %Z")
diff --git a/test/ikc3/time.out.expected b/test/ikc3/time.out.expected
new file mode 100644
index 0000000..db720df
--- /dev/null
+++ b/test/ikc3/time.out.expected
@@ -0,0 +1,4 @@
+%Time{tv_sec: (Sw) 0, tv_nsec: (Sw) 0}
+%Time{tv_sec: (Sw) 0, tv_nsec: (Sw) 0}
+Str.ftime(%Time{tv_sec: (Sw) 0, tv_nsec: (Sw) 0}, "%a, %b %d %G %T %Z")
+"Thu, Jan 01 1970 00:00:00 GMT"
diff --git a/test/ikc3/time.ret.expected b/test/ikc3/time.ret.expected
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/test/ikc3/time.ret.expected
@@ -0,0 +1 @@
+0