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

Re: [PATCH] ztrftime(): Fix truncation for %.



dana wrote on Mon, Dec 24, 2018 at 10:24:16 -0600:
> On 24 Dec 2018, at 06:45, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> >Shouldn't the former print [124]?
> 
> No, %. only uses the nanoseconds value given to ztrftime(), which is
> implicitly 0 in the former case. It's not substituting an arbitrary series of
> inputs in the style of printf, it only takes those two time arguments to
> ztrftime().

D'oh, thanks.

> +++ b/Src/utils.c
> @@ -3334,19 +3334,22 @@ morefmt:
>  #endif
>  	    switch (*fmt++) {
>  	    case '.':
> +	    {
>  		if (digs > 9)
>  		    digs = 9;
> +		if (ztrftimebuf(&bufsize, digs))
> +		    return -1;
> +		long fnsec = nsec;
>  		if (digs < 9) {
>  		    int trunc;
>  		    for (trunc = 8 - digs; trunc; trunc--)
> +			fnsec /= 10;
> +		    fnsec = (nsec < 999999500 ? (fnsec + 5) : fnsec) / 10;
>  		}

Why the magic number 999999500?  Why not 999999950 or 999995000 or
99950?  And why would it a problem to add 5 when nsec=999999500?

> +		sprintf(buf, "%0*ld", digs, fnsec);
>  		buf += digs;
>  		break;
> +	    }
>  	    case '\0':
>  		/* Guard against premature end of string */
>  		*buf++ = '%';



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