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

Re: TIMEFMT variable - request for extension



> 
> 
> Dear Zsh Users,
> 	Does anyone know of a simple way to get the format of the time report
> to print in HH:MM:SS format? I have read the zshparam.1 man page and don't
> see such an option. The closest thing to it is %E, the elapsed user time in 
> seconds. However, seeing  "3600.12" seconds is not as user friendly as
> "1:00:00.12". Thanks. :-)

I also missed this feature. Here is a patch to implement this. If you write a
star between the percent sign and the E, U or S flags, the time will be
printed in hh:mm:ss.ttt if it's greater that 1 hour. In mm.ss.ttt if it's at
least 1 minute, and only the number of seconts will be pronted if it's less
than a minute. Eg.

% TIMEFMT='%J: %U+%S, %P CPU, %*E total'
% time sleep 61
sleep 61: 0.020s+0.040s, 0% CPU, 1:01.055 total

The output format can be asily altered by modifying the three printf's in the
printhhmmss function.

The patch to the manual also tells the fact that only a few of the documented
TIMEFMT escapes are usable.

The source patch replaces an (s++, *s) with (*++s) since the later is more
clear I think.

Zoltan

rcsdiff -qc -kk -r1.7 -r1.8 Doc/zshparam.1
*** Doc/zshparam.1
--- Doc/zshparam.1	1995/06/08 01:40:56
***************
*** 711,716 ****
--- 711,720 ----
  .PD
  .PP
  .PD 0
+ Currently, only the E, U, S, P and J flags are implemented.
+ A star may be inserted between the percent sign and flags printing time.
+ This cause the time to be printed in hh:mm:ss.ttt format (hours and
+ minutes are only printed if they are not zero).
  .TP
  .B TMOUT
  If this parameter is nonzero, the shell will receive an \fBALRM\fP
rcsdiff -qc -kk -r1.1 -r1.2 Src/jobs.c
*** Src/jobs.c
--- Src/jobs.c	1995/06/09 17:20:56
***************
*** 641,646 ****
--- 641,665 ----
  
  /**/
  void
+ printhhmmss(double secs)
+ {
+     int intsecs = secs;
+     int tsecs = 1000 * (secs - intsecs);
+     int mins = intsecs / 60;
+     int hours = mins / 60;
+ 
+     intsecs -= 60 * mins;
+     mins -= 60 * hours;
+     if (hours)
+ 	fprintf(stderr, "%d:%02d:%02d.%03d", hours, mins, intsecs, tsecs);
+     else if (mins)
+ 	fprintf(stderr,      "%d:%02d.%03d",        mins, intsecs, tsecs);
+     else
+ 	fprintf(stderr,         "%d.%03d",                intsecs, tsecs);
+ }
+ 
+ /**/
+ void
  printtime(struct timeval *real, struct timeinfo *ti, char *desc)
  {
      char *s;
***************
*** 676,682 ****
  
      for (s = (timefmt ? timefmt : DEFAULT_TIMEFMT); *s; s++)
  	if (*s == '%')
! 	    switch (s++, *s) {
  	    case 'E':
  		fprintf(stderr, "%4.3fs", elapsed_time);
  		break;
--- 695,701 ----
  
      for (s = (timefmt ? timefmt : DEFAULT_TIMEFMT); *s; s++)
  	if (*s == '%')
! 	    switch (*++s) {
  	    case 'E':
  		fprintf(stderr, "%4.3fs", elapsed_time);
  		break;
***************
*** 685,690 ****
--- 704,726 ----
  		break;
  	    case 'S':
  		fprintf(stderr, "%4.3fs", system_time);
+ 		break;
+ 	    case '*':
+ 		switch (*++s) {
+ 		case 'E':
+ 		    printhhmmss(elapsed_time);
+ 		    break;
+ 		case 'U':
+ 		    printhhmmss(user_time);
+ 		    break;
+ 		case 'S':
+ 		    printhhmmss(system_time);
+ 		    break;
+ 		default:
+ 		    fprintf(stderr, "%%*");
+ 		    s--;
+ 		    break;
+ 		}
  		break;
  	    case 'P':
  		fprintf(stderr, "%d%%", percent);



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