1
0
mirror of git://git.code.sf.net/p/zsh/code synced 2024-09-28 06:49:49 +02:00

43953: Fix rounding/truncation error in %. time-format specifier

Also fixes an issue where %. couldn't be used more than once in a format
string without strange results

Tweaked very slightly per workers/43954
This commit is contained in:
dana 2018-12-29 05:22:34 -06:00
parent f64cd71d44
commit 162c198aab
3 changed files with 36 additions and 6 deletions

View File

@ -1,3 +1,8 @@
2018-12-29 dana <dana@dana.is>
* 43953 (tweaked per 43954): Src/utils.c, Test/V09datetime.ztst:
Fix rounding/truncation error in %. time-format specifier
2018-12-24 dana <dana@dana.is>
* 43935 (tweaked): Src/Modules/datetime.c, Test/V09datetime.ztst:

View File

@ -3334,19 +3334,28 @@ morefmt:
#endif
switch (*fmt++) {
case '.':
if (ztrftimebuf(&bufsize, digs))
return -1;
{
long fnsec = nsec;
if (digs > 9)
digs = 9;
if (ztrftimebuf(&bufsize, digs))
return -1;
if (digs < 9) {
int trunc;
for (trunc = 8 - digs; trunc; trunc--)
nsec /= 10;
nsec = (nsec + 8) / 10;
long max = 100000000;
for (trunc = 8 - digs; trunc; trunc--) {
max /= 10;
fnsec /= 10;
}
max -= 1;
fnsec = (fnsec + 5) / 10;
if (fnsec > max)
fnsec = max;
}
sprintf(buf, "%0*ld", digs, nsec);
sprintf(buf, "%0*ld", digs, fnsec);
buf += digs;
break;
}
case '\0':
/* Guard against premature end of string */
*buf++ = '%';

View File

@ -114,3 +114,19 @@
strftime -r '%Y' 2> /dev/null
1:-r timestring not optional
# This tests rounding up and the use of repeated %.s
strftime '%Y-%m-%d %H:%M:%S.%3..%3.' 1012615322 $(( 999_999 ))
# These test the ceiling on rounding up
for 1 in %. %1. %3. %6. %9. %12.; do
print -rn - "$1 "
strftime "%Y-%m-%d %H:%M:%S.$1" 1012615322 $(( 999_999_999 ))
done
0:%. truncation
>2002-02-02 02:02:02.001.001
>%. 2002-02-02 02:02:02.999
>%1. 2002-02-02 02:02:02.9
>%3. 2002-02-02 02:02:02.999
>%6. 2002-02-02 02:02:02.999999
>%9. 2002-02-02 02:02:02.999999999
>%12. 2002-02-02 02:02:02.999999999