From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7427 invoked by alias); 30 Jul 2014 13:16:24 -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: 32920 Received: (qmail 29060 invoked from network); 30 Jul 2014 13:16:20 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=sOcqxvcFb/GAjDZfvnuzmtC+h+PhB3ouwaAn1rMcX8c=; b=IuHFicUgvViZpS5gOINakBfzM/iCmuWKYw1inZF/Xenuan9Bx53DxYBPaY42vCr1xS 2nkI92SFE7pDQGrVcpT+p/NzE3oTy8TU0CALCwXLht30VlyIy3LhSuc9XA2yrCGBe3NR EtBxMWwukfDp/SyzKmdJl0zqIylzwQiqI/+q35q/pwsF+XnLSbmZ+YCOm9SSGO489EJ7 UEGtEE/07gKBfrHKIWUjNZmJwlJ28K5N2qnAPJvbIYcHNXsXZ5nDc/dZoFKeputzdLzS xcoDqDj6E6eAt8cACyPT/NNdTHVZnbMxeGItlqeGf2LvwIm9e8flNJZN4wUb+WuBv740 PnUQ== MIME-Version: 1.0 X-Received: by 10.52.251.195 with SMTP id zm3mr7700988vdc.36.1406726176595; Wed, 30 Jul 2014 06:16:16 -0700 (PDT) Sender: ethersoft@gmail.com In-Reply-To: <20140730114651.5687f4df@pwslap01u.europe.root.pri> References: <20140730114651.5687f4df@pwslap01u.europe.root.pri> Date: Wed, 30 Jul 2014 06:16:16 -0700 X-Google-Sender-Auth: KzOn8Pa8-DwAdIn_Q8N_fRicJys Message-ID: Subject: Re: PATCH: ztrftime %. for fractions of a second in prompts From: Vin Shelton To: Peter Stephenson Cc: "Zsh Hackers' List" Content-Type: multipart/alternative; boundary=001a1136030a0d63f704ff68f6a2 --001a1136030a0d63f704ff68f6a2 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Dear Peter, On my Mint 17 system, the latest code fails to compile with gcc 4.8.2: gcc -c -I. -I../../Src -I/opt/src/zsh-2014-07-30/Src -I/opt/src/zsh-2014-07-30/Src/Zle -I/opt/src/zsh-2014-07-30/Src/Builtins -DHAVE_CONFIG_H -DMODULE -Wall -Wmissing-prototypes -O2 -fPIC -o sched..o /opt/src/zsh-2014-07-30/Src/Builtins/sched.c /opt/src/zsh-2014-07-30/Src/Builtins/sched.c: In function =E2=80=98bin_sche= d=E2=80=99: /opt/src/zsh-2014-07-30/Src/Builtins/sched.c:214:6: error: too few arguments to function =E2=80=98ztrftime=E2=80=99 ztrftime(tbuf, 40, "%a %b %e %k:%M:%S", tmp); ^ In file included from ./../../Src/zsh.mdh:62:0, from ./sched.mdh:17, from /opt/src/zsh-2014-07-30/Src/Builtins/sched.c:30: ./../../Src/utils.epro:80:32: note: declared here extern mod_import_function int ztrftime _((char*buf,int bufsize,char*fmt,struct tm*tm,long usec)); ^ make[3]: *** [sched..o] Error 1 make[3]: Leaving directory `/opt/build/zsh-2014-07-30/Src/Builtins' make[2]: *** [modules] Error 1 make[2]: Leaving directory `/opt/build/zsh-2014-07-30/Src' make[1]: *** [modules] Error 2 make[1]: Leaving directory `/opt/build/zsh-2014-07-30/Src' make: *** [all] Error 1 Please let me know if you need more info. - Vin On Wed, Jul 30, 2014 at 3:46 AM, Peter Stephenson wrote: > I was trying to get some timings in prompts to better than a second --- > not particularly useful in a regular shell prompt, but very useful in > the prompts provided by e.g. the TCP functions. > > I was shocked! shocked! to find this appears not to be possible. > > I've used %. for this as it's definitely not POSIX and is very easy to > remember. You can specify 1 to 6 digits between the '%' and the '.'. > > This doesn't appear to be useful in non-prompt calls to ztrftime() since > the fractions of a second aren't available. > > pws > > > diff --git a/Doc/Zsh/prompt.yo b/Doc/Zsh/prompt.yo > index eab15d2..36f351b 100644 > --- a/Doc/Zsh/prompt.yo > +++ b/Doc/Zsh/prompt.yo > @@ -185,6 +185,13 @@ sitem(tt(%K))(the hour of the day on the 24-hour > clock) > sitem(tt(%L))(the hour of the day on the 12-hour clock) > endsitem() > > +In addition, if the system supports the POSIX tt(gettimeofday) system > +call, tt(%.) provides decimal fractions of a second since the epoch with > +leading zeroes. By default three decimal places are provided, but a > +number of digits up to 6 may be given following the tt(%); hence tt(%6.) > +outputs microseconds. A typical example of this is the format > +`tt(%D{%H:%M:%S.%.})'. > + > The GNU extension that a `tt(-)' between the tt(%) and the > format character causes a leading zero or space to be stripped > is handled directly by the shell for the format characters tt(d), tt(f), > diff --git a/Src/builtin.c b/Src/builtin.c > index 34a5296..a2a3ad7 100644 > --- a/Src/builtin.c > +++ b/Src/builtin.c > @@ -1731,7 +1731,7 @@ fclist(FILE *f, Options ops, zlong first, zlong las= t, > if (tdfmt !=3D NULL) { > struct tm *ltm; > ltm =3D localtime(&ent->stim); > - if (ztrftime(timebuf, 256, tdfmt, ltm)) > + if (ztrftime(timebuf, 256, tdfmt, ltm, 0L)) > fprintf(f, "%s ", timebuf); > } > /* display the time taken by the command, if required */ > diff --git a/Src/prompt.c b/Src/prompt.c > index 95a7d49..c16d781 100644 > --- a/Src/prompt.c > +++ b/Src/prompt.c > @@ -270,6 +270,8 @@ putpromptchar(int doprint, int endchar, unsigned int > *txtchangep) > char *ss, *hostnam; > int t0, arg, test, sep, j, numjobs; > struct tm *tm; > + struct timezone dummy_tz; > + struct timeval tv; > time_t timet; > Nameddir nd; > > @@ -636,8 +638,8 @@ putpromptchar(int doprint, int endchar, unsigned int > *txtchangep) > tmfmt =3D "%l:%M%p"; > break; > } > - timet =3D time(NULL); > - tm =3D localtime(&timet); > + gettimeofday(&tv, &dummy_tz); > + tm =3D localtime(&tv.tv_sec); > /* > * Hack because strftime won't say how > * much space it actually needs. Try to add it > @@ -647,7 +649,7 @@ putpromptchar(int doprint, int endchar, unsigned int > *txtchangep) > */ > for(j =3D 0, t0 =3D strlen(tmfmt)*8; j < 3; j++, t0*= =3D2) { > addbufspc(t0); > - if (ztrftime(bv->bp, t0, tmfmt, tm) >=3D 0) > + if (ztrftime(bv->bp, t0, tmfmt, tm, tv.tv_usec) >= =3D > 0) > break; > } > /* There is enough room for this because addbufspc(t0= ) > diff --git a/Src/utils.c b/Src/utils.c > index cef2abe..aa978e6 100644 > --- a/Src/utils.c > +++ b/Src/utils.c > @@ -2710,7 +2710,7 @@ ztrftimebuf(int *bufsizeptr, int decr) > > /**/ > mod_export int > -ztrftime(char *buf, int bufsize, char *fmt, struct tm *tm) > +ztrftime(char *buf, int bufsize, char *fmt, struct tm *tm, long usec) > { > int hr12; > #ifdef HAVE_STRFTIME > @@ -2729,6 +2729,7 @@ ztrftime(char *buf, int bufsize, char *fmt, struct > tm *tm) > while (*fmt) > if (*fmt =3D=3D '%') { > int strip; > + int digs =3D 3; > > fmt++; > if (*fmt =3D=3D '-') { > @@ -2736,6 +2737,17 @@ ztrftime(char *buf, int bufsize, char *fmt, struct > tm *tm) > fmt++; > } else > strip =3D 0; > + if (idigit(*fmt)) { > + /* Digit --- only useful with . */ > + char *dstart =3D fmt; > + char *dend =3D fmt+1; > + while (idigit(*dend)) > + dend++; > + if (*dend =3D=3D '.') { > + fmt =3D dend; > + digs =3D atoi(dstart); > + } > + } > /* > * Assume this format will take up at least two > * characters. Not always true, but if that matters > @@ -2745,6 +2757,20 @@ ztrftime(char *buf, int bufsize, char *fmt, struct > tm *tm) > if (ztrftimebuf(&bufsize, 2)) > return -1; > switch (*fmt++) { > + case '.': > + if (ztrftimebuf(&bufsize, digs)) > + return -1; > + if (digs > 6) > + digs =3D 6; > + if (digs < 6) { > + int trunc; > + for (trunc =3D 5 - digs; trunc; trunc--) > + usec /=3D 10; > + usec =3D (usec + 5) / 10; > + } > + sprintf(buf, "%0*ld", digs, usec); > + buf +=3D digs; > + break; > case 'd': > if (tm->tm_mday > 9 || !strip) > *buf++ =3D '0' + tm->tm_mday / 10; > diff --git a/Src/watch.c b/Src/watch.c > index 5231579..8dea0b4 100644 > --- a/Src/watch.c > +++ b/Src/watch.c > @@ -330,7 +330,7 @@ watchlog2(int inout, WATCH_STRUCT_UTMP *u, char *fmt, > int prnt, int fini) > } > timet =3D getlogtime(u, inout); > tm =3D localtime(&timet); > - ztrftime(buf, 40, fm2, tm); > + ztrftime(buf, 40, fm2, tm, 0L); > printf("%s", (*buf =3D=3D ' ') ? buf + 1 : buf); > break; > case '%': > --001a1136030a0d63f704ff68f6a2--