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

PATCH: strftime -s



This adds the option `-s scalar' to strftime to do an assignment instead
of a print.  Otherwise there's no gain over using date when assigning to
a parameter.

It also fixes a bug that the EPOCHSECONDS wasn't unset when the module
was unloaded, which could have nasty effects.  There ought to be
something in the development guide about this.  (Ideally, there ought to
be an easier way of removing module parameters --- it's a bit hairy at
the moment.)

Index: Src/Modules/datetime.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/datetime.c,v
retrieving revision 1.6
diff -u -r1.6 datetime.c
--- Src/Modules/datetime.c	8 Oct 2003 13:37:13 -0000	1.6
+++ Src/Modules/datetime.c	22 Jan 2004 17:24:28 -0000
@@ -35,10 +35,18 @@
 bin_strftime(char *nam, char **argv, Options ops, int func)
 {
     int bufsize, x;
-    char *endptr = NULL, *buffer;
+    char *endptr = NULL, *scalar = NULL, *buffer;
     time_t secs;
     struct tm *t;
 
+    if (OPT_ISSET(ops,'s')) {
+	scalar = OPT_ARG(ops, 's');
+	if (!isident(scalar)) {
+	    zwarnnam(nam, "not an identifier: %s", scalar, 0);
+	    return 1;
+	}
+    }
+
     secs = (time_t)strtoul(argv[1], &endptr, 10);
     if (secs == ULONG_MAX) {
 	zwarnnam(nam, "%s: %e", argv[1], errno);
@@ -58,7 +66,11 @@
 	buffer = zrealloc(buffer, bufsize *= 2);
     }
 
-    printf("%s\n", buffer);
+    if (scalar) {
+	setsparam(scalar, ztrdup(buffer));
+    } else {
+	printf("%s\n", buffer);
+    }
     zfree(buffer, bufsize);
 
     return 0;
@@ -71,7 +83,7 @@
 }
 
 static struct builtin bintab[] = {
-    BUILTIN("strftime",    0, bin_strftime,    2,   2, 0, NULL, NULL),
+    BUILTIN("strftime",    0, bin_strftime,    2,   2, 0, "s:", NULL),
 };
 
 static struct paramdef patab[] = {
@@ -99,7 +111,14 @@
 int
 cleanup_(Module m)
 {
+    Param pm;
+
     deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
+    pm = (Param) paramtab->getnode(paramtab, "EPOCHSECONDS");
+    if (pm && (pm->flags & PM_SPECIAL)) {
+	pm->flags &= ~PM_READONLY;
+	unsetparam_pm(pm, 0, 1);
+    }
     return 0;
 }
 
Index: Doc/Zsh/mod_datetime.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_datetime.yo,v
retrieving revision 1.1
diff -u -r1.1 mod_datetime.yo
--- Doc/Zsh/mod_datetime.yo	25 Oct 2003 17:38:19 -0000	1.1
+++ Doc/Zsh/mod_datetime.yo	22 Jan 2004 17:24:29 -0000
@@ -6,9 +6,12 @@
 startitem()
 findex(strftime)
 cindex(date string, printing)
-item(tt(strftime) var(format) var(epochtime) )(
+item(tt(strftime) [ tt(-s) var(scalar) ] var(format) var(epochtime) )(
 Output the date denoted by var(epochtime) in the var(format)
 specified.
+
+If tt(-s) var(scalar) is given, assign the date to var(scalar) instead
+of printing it.
 )
 enditem()
 

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



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