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

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



dana wrote on Mon, 24 Dec 2018 03:16 -0600:
> Not directly related, but in reviewing workers/43932 further i found that the
> way ztrftime() handles truncation for %. is problematic:
> 
>   % strftime '%s.%9.' $EPOCHSECONDS $(( 999_999_999 ))
>   1545638724.999999999
>   % strftime '%s.%6.' $EPOCHSECONDS $(( 999_999_000 ))
>   1545638984.999999
>   % strftime '%s.%6.' $EPOCHSECONDS $(( 999_999_999 ))
>   1545638724.100000

That's a very odd sequence of $EPOCHSECONDS values. :P

Also, in (unpatched) master:

% strftime '[%3.]' 123456789
[000]
% strftime '[%s][%3.]' 0 123456789
[0][124]
% 

Shouldn't the former print [124]?

> I'm not very good at maths — is there a reason it shouldn't just be a straight
> power-of-ten division like this?

Using straight division likes this makes it do truncation; in the code
before this patch, the +8 makes it so nsec=11152 would print '1116'
but nsec=11151 would print '1115'.  (The +8 should probably be +5 ---
half the radix.)  999999999 is an edge case since it rounds up "to the
next second".

> +++ b/Src/utils.c
> @@ -3340,9 +3340,8 @@ morefmt:
>  		    digs = 9;
>  		if (digs < 9) {
>  		    int trunc;
> -		    for (trunc = 8 - digs; trunc; trunc--)
> +		    for (trunc = 9 - digs; trunc; trunc--)
>  			nsec /= 10;
> -		    nsec = (nsec + 8) / 10;
>  		}
>  		sprintf(buf, "%0*ld", digs, nsec);
>  		buf += digs;



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