zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: "Zsh Hackers' List" <zsh-workers@zsh.org>
Subject: Re: PATCH: zsh/datetime $EPOCHREALTIME
Date: Thu, 11 Aug 2011 19:33:32 +0100	[thread overview]
Message-ID: <20110811193332.123ba2d9@pws-pc.ntlworld.com> (raw)
In-Reply-To: <20110810171946.467b15b5@pwslap01u.europe.root.pri>

Index: Doc/Zsh/mod_datetime.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_datetime.yo,v
retrieving revision 1.4
diff -p -u -r1.4 mod_datetime.yo
--- Doc/Zsh/mod_datetime.yo	10 Aug 2011 11:31:19 -0000	1.4
+++ Doc/Zsh/mod_datetime.yo	11 Aug 2011 18:24:33 -0000
@@ -30,7 +30,8 @@ in seconds if tt(-r) is given) to var(sc
 )
 enditem()
 
-The tt(zsh/datetime) module makes available several parameters:
+The tt(zsh/datetime) module makes available several parameters;
+all are readonly:
 
 startitem()
 vindex(EPOCHREALTIME)
@@ -46,4 +47,18 @@ item(tt(EPOCHSECONDS))(
 An integer value representing the number of seconds since the
 epoch.
 )
+vindex(epochtime)
+item(tt(epochtime))(
+An array value containing the number of seconds since the epoch
+in the first element and the remainder of the time since the epoch
+in nanoseconds in the second element.  To ensure the two elements
+are consistent the array should be copied or otherwise referenced
+as a single substitution before the values are used.  The following
+idiom may be used:
+
+example(for secs nsecs in $epochtime; do
+  ...
+done)
+
+)
 enditem()
Index: Src/Modules/datetime.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/datetime.c,v
retrieving revision 1.23
diff -p -u -r1.23 datetime.c
--- Src/Modules/datetime.c	10 Aug 2011 11:31:19 -0000	1.23
+++ Src/Modules/datetime.c	11 Aug 2011 18:24:33 -0000
@@ -173,6 +173,45 @@ getcurrentrealtime(UNUSED(Param pm))
 #endif
 }
 
+static char **
+getcurrenttime(UNUSED(Param pm))
+{
+    char **arr;
+    char buf[DIGBUFSIZE];
+
+#ifdef HAVE_CLOCK_GETTIME
+    struct timespec now;
+
+    if (clock_gettime(CLOCK_REALTIME, &now) < 0) {
+	zwarn("EPOCHREALTIME: unable to retrieve time: %e", errno);
+	return NULL;
+    }
+
+    arr = (char **)zhalloc(3 * sizeof(*arr));
+    sprintf(buf, "%ld", (long)now.tv_sec);
+    arr[0] = dupstring(buf);
+    sprintf(buf, "%ld", now.tv_nsec);
+    arr[1] = dupstring(buf);
+    arr[2] = NULL;
+
+    return arr;
+#else
+    struct timeval now;
+    struct timezone dummy_tz;
+
+    gettimeofday(&now, &dummy_tz);
+
+    arr = (char **)zhalloc(3 * sizeof(*arr));
+    sprintf(buf, "%ld", (long)now.tv_sec);
+    arr[0] = dupstring(buf);
+    sprintf(buf, "%ld", (long)now.tv_usec * 1000);
+    arr[1] = dupstring(buf);
+    arr[2] = NULL;
+
+    return arr;
+#endif
+}
+
 static struct builtin bintab[] = {
     BUILTIN("strftime",    0, bin_strftime,    2,   2, 0, "qrs:", NULL),
 };
@@ -183,11 +222,16 @@ static const struct gsu_integer epochsec
 static const struct gsu_float epochrealtime_gsu =
 { getcurrentrealtime, NULL, stdunsetfn };
 
+static const struct gsu_array epochtime_gsu =
+{ getcurrenttime, NULL, stdunsetfn };
+
 static struct paramdef patab[] = {
     SPECIALPMDEF("EPOCHSECONDS", PM_INTEGER|PM_READONLY,
 		 &epochseconds_gsu, NULL, NULL),
     SPECIALPMDEF("EPOCHREALTIME", PM_FFLOAT|PM_READONLY,
-		 &epochrealtime_gsu, NULL, NULL)
+		 &epochrealtime_gsu, NULL, NULL),
+    SPECIALPMDEF("epochtime", PM_ARRAY|PM_READONLY,
+		 &epochtime_gsu, NULL, NULL)
 };
 
 static struct features module_features = {

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


  reply	other threads:[~2011-08-11 18:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-10 11:21 Peter Stephenson
2011-08-10 11:37 ` Mikael Magnusson
2011-08-10 11:41   ` Peter Stephenson
2011-08-10 15:30     ` Bart Schaefer
2011-08-10 15:38       ` Mikael Magnusson
2011-08-10 16:19         ` Peter Stephenson
2011-08-11 18:33           ` Peter Stephenson [this message]
2011-08-10 15:41       ` Peter Stephenson
2011-08-10 16:08         ` Bart Schaefer
2011-08-10 16:13           ` Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110811193332.123ba2d9@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).