From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28952 invoked by alias); 11 Aug 2011 18:34:00 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 29674 Received: (qmail 8389 invoked from network); 11 Aug 2011 18:33:47 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received-SPF: neutral (ns1.primenet.com.au: 74.125.82.171 is neither permitted nor denied by SPF record at ntlworld.com) X-ProxyUser-IP: 86.27.188.118 Date: Thu, 11 Aug 2011 19:33:32 +0100 From: Peter Stephenson To: "Zsh Hackers' List" Subject: Re: PATCH: zsh/datetime $EPOCHREALTIME Message-ID: <20110811193332.123ba2d9@pws-pc.ntlworld.com> In-Reply-To: <20110810171946.467b15b5@pwslap01u.europe.root.pri> References: <20110810122137.3a3e4c00@pwslap01u.europe.root.pri> <20110810124120.0479177b@pwslap01u.europe.root.pri> <110810083017.ZM4722@torch.brasslantern.com> <20110810171946.467b15b5@pwslap01u.europe.root.pri> X-Mailer: Claws Mail 3.7.9 (GTK+ 2.24.4; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 Web page now at http://homepage.ntlworld.com/p.w.stephenson/