* PATCH: ztrftime %. for fractions of a second in prompts
@ 2014-07-30 10:46 Peter Stephenson
2014-07-30 11:21 ` Peter Stephenson
2014-07-30 13:16 ` Vin Shelton
0 siblings, 2 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-07-30 10:46 UTC (permalink / raw)
To: Zsh Hackers' List
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 last,
if (tdfmt != NULL) {
struct tm *ltm;
ltm = 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 = "%l:%M%p";
break;
}
- timet = time(NULL);
- tm = localtime(&timet);
+ gettimeofday(&tv, &dummy_tz);
+ tm = 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 = 0, t0 = strlen(tmfmt)*8; j < 3; j++, t0*=2) {
addbufspc(t0);
- if (ztrftime(bv->bp, t0, tmfmt, tm) >= 0)
+ if (ztrftime(bv->bp, t0, tmfmt, tm, tv.tv_usec) >= 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 == '%') {
int strip;
+ int digs = 3;
fmt++;
if (*fmt == '-') {
@@ -2736,6 +2737,17 @@ ztrftime(char *buf, int bufsize, char *fmt, struct tm *tm)
fmt++;
} else
strip = 0;
+ if (idigit(*fmt)) {
+ /* Digit --- only useful with . */
+ char *dstart = fmt;
+ char *dend = fmt+1;
+ while (idigit(*dend))
+ dend++;
+ if (*dend == '.') {
+ fmt = dend;
+ digs = 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 = 6;
+ if (digs < 6) {
+ int trunc;
+ for (trunc = 5 - digs; trunc; trunc--)
+ usec /= 10;
+ usec = (usec + 5) / 10;
+ }
+ sprintf(buf, "%0*ld", digs, usec);
+ buf += digs;
+ break;
case 'd':
if (tm->tm_mday > 9 || !strip)
*buf++ = '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 = getlogtime(u, inout);
tm = localtime(&timet);
- ztrftime(buf, 40, fm2, tm);
+ ztrftime(buf, 40, fm2, tm, 0L);
printf("%s", (*buf == ' ') ? buf + 1 : buf);
break;
case '%':
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: PATCH: ztrftime %. for fractions of a second in prompts
2014-07-30 10:46 PATCH: ztrftime %. for fractions of a second in prompts Peter Stephenson
@ 2014-07-30 11:21 ` Peter Stephenson
2014-07-30 13:16 ` Vin Shelton
1 sibling, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-07-30 11:21 UTC (permalink / raw)
To: Peter Stephenson; +Cc: Zsh Hackers' List
On Wed, 30 Jul 2014 11:46:51 +0100
Peter Stephenson <p.stephenson@samsung.com> 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.
This requires some further massaging.
pws
diff --git a/Doc/Zsh/tcpsys.yo b/Doc/Zsh/tcpsys.yo
index 599335b..1e26054 100644
--- a/Doc/Zsh/tcpsys.yo
+++ b/Doc/Zsh/tcpsys.yo
@@ -666,6 +666,10 @@ expression `tt(%c)' expands to 1 if the session being read is the current
session, else 0; this is most useful in ternary expressions such as
`tt(%LPAR()c.-.PLUS()RPAR())' which outputs `tt(PLUS())' if the session is
the current one, else `tt(-)'.
+
+If the prompt starts with tt(%P), this is stripped and the complete
+result of the previous stage is passed through standard prompt tt(%)-style
+formatting before being output.
)
vindex(TCP_READ_DEBUG)
item(tt(TCP_READ_DEBUG))(
diff --git a/Functions/TCP/tcp_output b/Functions/TCP/tcp_output
index 781c46c..22e818e 100644
--- a/Functions/TCP/tcp_output
+++ b/Functions/TCP/tcp_output
@@ -35,6 +35,9 @@ if [[ -n $tprompt ]]; then
cursess="c:0"
fi
zformat -f REPLY $tprompt "s:$sess" "f:$read_fd" $cursess
+ if [[ $REPLY = %P* ]]; then
+ REPLY=${(%)${REPLY##%P}}
+ fi
# We will pass this back up.
REPLY="$REPLY$*"
else
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: PATCH: ztrftime %. for fractions of a second in prompts
2014-07-30 10:46 PATCH: ztrftime %. for fractions of a second in prompts Peter Stephenson
2014-07-30 11:21 ` Peter Stephenson
@ 2014-07-30 13:16 ` Vin Shelton
2014-07-30 13:24 ` Peter Stephenson
1 sibling, 1 reply; 5+ messages in thread
From: Vin Shelton @ 2014-07-30 13:16 UTC (permalink / raw)
To: Peter Stephenson; +Cc: Zsh Hackers' List
[-- Attachment #1: Type: text/plain, Size: 7737 bytes --]
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 ‘bin_sched’:
/opt/src/zsh-2014-07-30/Src/Builtins/sched.c:214:6: error: too few
arguments to function ‘ztrftime’
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 <p.stephenson@samsung.com>
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 last,
> if (tdfmt != NULL) {
> struct tm *ltm;
> ltm = 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 = "%l:%M%p";
> break;
> }
> - timet = time(NULL);
> - tm = localtime(&timet);
> + gettimeofday(&tv, &dummy_tz);
> + tm = 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 = 0, t0 = strlen(tmfmt)*8; j < 3; j++, t0*=2) {
> addbufspc(t0);
> - if (ztrftime(bv->bp, t0, tmfmt, tm) >= 0)
> + if (ztrftime(bv->bp, t0, tmfmt, tm, tv.tv_usec) >=
> 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 == '%') {
> int strip;
> + int digs = 3;
>
> fmt++;
> if (*fmt == '-') {
> @@ -2736,6 +2737,17 @@ ztrftime(char *buf, int bufsize, char *fmt, struct
> tm *tm)
> fmt++;
> } else
> strip = 0;
> + if (idigit(*fmt)) {
> + /* Digit --- only useful with . */
> + char *dstart = fmt;
> + char *dend = fmt+1;
> + while (idigit(*dend))
> + dend++;
> + if (*dend == '.') {
> + fmt = dend;
> + digs = 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 = 6;
> + if (digs < 6) {
> + int trunc;
> + for (trunc = 5 - digs; trunc; trunc--)
> + usec /= 10;
> + usec = (usec + 5) / 10;
> + }
> + sprintf(buf, "%0*ld", digs, usec);
> + buf += digs;
> + break;
> case 'd':
> if (tm->tm_mday > 9 || !strip)
> *buf++ = '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 = getlogtime(u, inout);
> tm = localtime(&timet);
> - ztrftime(buf, 40, fm2, tm);
> + ztrftime(buf, 40, fm2, tm, 0L);
> printf("%s", (*buf == ' ') ? buf + 1 : buf);
> break;
> case '%':
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: PATCH: ztrftime %. for fractions of a second in prompts
2014-07-30 13:16 ` Vin Shelton
@ 2014-07-30 13:24 ` Peter Stephenson
2014-07-30 13:56 ` Vin Shelton
0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2014-07-30 13:24 UTC (permalink / raw)
To: Zsh Hackers' List
On Wed, 30 Jul 2014 06:16:16 -0700
Vin Shelton <acs@alumni.princeton.edu> wrote:
> Dear Peter,
>
> On my Mint 17 system, the latest code fails to compile with gcc 4.8.2:
I updated but didn't save three files in subdirectories --- that's fixed
now.
pws
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: PATCH: ztrftime %. for fractions of a second in prompts
2014-07-30 13:24 ` Peter Stephenson
@ 2014-07-30 13:56 ` Vin Shelton
0 siblings, 0 replies; 5+ messages in thread
From: Vin Shelton @ 2014-07-30 13:56 UTC (permalink / raw)
To: Peter Stephenson; +Cc: Zsh Hackers' List
[-- Attachment #1: Type: text/plain, Size: 420 bytes --]
Yes, thank you; sources built and all tests pass.
- Vin
On Wed, Jul 30, 2014 at 6:24 AM, Peter Stephenson <p.stephenson@samsung.com>
wrote:
> On Wed, 30 Jul 2014 06:16:16 -0700
> Vin Shelton <acs@alumni.princeton.edu> wrote:
> > Dear Peter,
> >
> > On my Mint 17 system, the latest code fails to compile with gcc 4.8.2:
>
> I updated but didn't save three files in subdirectories --- that's fixed
> now.
>
> pws
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-07-30 13:56 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-30 10:46 PATCH: ztrftime %. for fractions of a second in prompts Peter Stephenson
2014-07-30 11:21 ` Peter Stephenson
2014-07-30 13:16 ` Vin Shelton
2014-07-30 13:24 ` Peter Stephenson
2014-07-30 13:56 ` Vin Shelton
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).