1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 19:52:20 +00:00

Fix failure to zero pad with 'printf %(%0l)T' (re: 9526b3fa) (#107)

src/lib/libast/tm/tmxfmt.c:
- Making %l and %k aliases to %_I and %_H caused zero padding with
  %0l and %0k to fail. Fix that by fully implementing %l and %k
  without 'goto push'. This duplicates code from %I and %H, but it
  is necessary for these formats to work correctly when zero padded.

src/cmd/ksh93/tests/builtins.sh:
- Add a regression test for manually specifying blank and zero
  padding with sixteen different formats.
This commit is contained in:
Johnothan King 2020-08-05 09:52:21 -07:00 committed by GitHub
parent 07b240d4f9
commit 83996d5a8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 6 deletions

View file

@ -797,6 +797,25 @@ unset foo
[[ $(printf '%(%f)T') == $(printf '%(%Y.%m.%d-%H:%M:%S)T') ]] || err_exit 'date format %f is not the same as %Y.%m.%d-%H:%M:%S'
[[ $(printf '%(%q)T') == $(printf '%(%Qz)T') ]] && err_exit 'date format %q is the same as %Qz'
# Test manually specified blank and zero padding with 'printf %T'
(
IFS=$'\n\t' # Preserve spaces in output
for i in d e H I j J k l m M N S U V W y; do
for f in ' ' 0; do
if [[ $f == ' ' ]]; then
padding='blank'
specify='_'
else
padding='zero'
specify='0'
fi
actual="$(printf "%(%${specify}${i})T" 'January 1 6AM 2001')"
expect="${f}${actual:1}"
[[ $expect != $actual ]] && err_exit "Specifying $padding padding with format '%$i' doesn't work (expected '$expect', got '$actual')"
done
done
)
# ======
# Test various AST getopts usage/manual outputs

View file

@ -316,9 +316,9 @@ tmxfmt(char* buf, size_t len, const char* format, Time_t t)
case 'J': /* Julian date (0 offset) */
cp = number(cp, ep, (long)tm->tm_yday, 3, width, pad);
continue;
case 'k': /* hour (0 - 23) with blank padding */
p = "%_H";
goto push;
case 'k': /* hour (0 - 23) with blank padding (can't be an alias to %_H) */
cp = number(cp, ep, (long)tm->tm_hour, -2, width, pad);
continue;
case 'K': /* (AST) largest to smallest */
switch (alt)
{
@ -333,9 +333,11 @@ tmxfmt(char* buf, size_t len, const char* format, Time_t t)
break;
}
goto push;
case 'l': /* hour (0 - 12) with blank padding */
p = "%_I";
goto push;
case 'l': /* hour (0 - 12) with blank padding (can't be an alias to %_I) */
if ((n = tm->tm_hour) > 12) n -= 12;
else if (n == 0) n = 12;
cp = number(cp, ep, (long)n, -2, width, pad);
continue;
case 'L': /* (AST) OBSOLETE use %Ql */
p = "%Ql";
goto push;