Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: strftime builtin



> The strftime formats are probably pretty useless without a strftime
> builtin.

This probably doesn't belong in the zsh/langinfo module.
Should it get its own module?

interim documentation: strftime format secs_since_epoch

Index: Src/Modules/langinfo.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/langinfo.c,v
retrieving revision 1.1
diff -u -r1.1 langinfo.c
--- Src/Modules/langinfo.c	19 Feb 2002 02:14:09 -0000	1.1
+++ Src/Modules/langinfo.c	30 Apr 2002 18:45:47 -0000
@@ -29,6 +29,7 @@
 
 #include "langinfo.mdh"
 #include "langinfo.pro"
+#include <time.h>
 
 static char langinfo_nam[] = "langinfo";
 
@@ -513,6 +514,42 @@
 /**/
 #endif /* HAVE_NL_LANGINFO */
 
+static int
+bin_strftime(char *nam, char **argv, char *ops, int func)
+{
+    int ret = 0, bufsize, x;
+    char *endptr = NULL, *buffer = NULL;
+    time_t secs;
+    struct tm *t;
+    size_t size;
+
+    secs = (time_t)strtoul(argv[1], &endptr, 10);
+    if (secs == ULONG_MAX) {
+	zwarnnam(nam, "%s: %e", argv[1], errno);
+	return 1;
+    } else if (*endptr != '\0') {
+	zwarnnam(nam, "%s: invalid decimal number", argv[1], 0);
+	return 1;
+    }
+
+    t = localtime(&secs);
+    bufsize = strlen(argv[0]) * 2;
+
+    for (x=1;x<4;x++) {
+	buffer = zrealloc(buffer, bufsize * x);
+        size = strftime(buffer, bufsize * x, argv[0], t);
+	if (size) x = 4;
+    }
+
+    printf("%s\n", buffer);
+    
+    return 0;
+}
+
+static struct builtin bintab[] = {
+    BUILTIN("strftime",    0, bin_strftime,    2,   2, 0, NULL, NULL),
+};
+
 /**/
 int
 setup_(Module m)
@@ -530,7 +567,8 @@
 #else
     unsetparam(langinfo_nam);
 #endif
-    return 0;
+    return !addbuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
+/*    return 0; */
 }
 
 /**/
@@ -546,6 +584,7 @@
 	unsetparam_pm(pm, 0, 1);
     }
 #endif
+    deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
     return 0;
 }
 
Index: Src/Modules/langinfo.mdd
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/langinfo.mdd,v
retrieving revision 1.1
diff -u -r1.1 langinfo.mdd
--- Src/Modules/langinfo.mdd	19 Feb 2002 02:14:09 -0000	1.1
+++ Src/Modules/langinfo.mdd	30 Apr 2002 18:45:47 -0000
@@ -4,5 +4,6 @@
 load=no
 
 autoparams="langinfo"
+autobins="strftime"
 
 objects="langinfo.o"



Messages sorted by: Reverse Date, Date, Thread, Author