Skip to content

Commit 9b89399

Browse files
committed
[v1.0] posix: don't zero-pad 2nds (re: 5c677a4, 70fc1da, b1a4131)
The POSIX mode now disables left-hand zero-padding of seconds in 'time'/'times' output. The standard specifies the output format quite exactly and zero-padding is not in it.
1 parent cfc7307 commit 9b89399

File tree

7 files changed

+31
-12
lines changed

7 files changed

+31
-12
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
33

44
Any uppercase BUG_* names are modernish shell bug IDs.
55

6+
2022-06-12:
7+
8+
- The POSIX mode now disables zero-padding of seconds in 'time'/'times' output.
9+
610
2022-06-09:
711

812
- The POSIX mode has been amended to use a UNIX pipe(2) instead of a

src/cmd/ksh93/bltins/misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ static void print_times(struct timeval utime, struct timeval stime)
519519
int st_min = stime.tv_sec / 60;
520520
int st_sec = stime.tv_sec % 60;
521521
int st_ms = stime.tv_usec / 1000;
522-
sfprintf(sfstdout, "%dm%02d%c%03ds %dm%02d%c%03ds\n",
522+
sfprintf(sfstdout, sh_isoption(SH_POSIX) ? "%dm%d%c%03ds %dm%d%c%03ds\n" : "%dm%02d%c%03ds %dm%02d%c%03ds\n",
523523
ut_min, ut_sec, sh.radixpoint, ut_ms, st_min, st_sec, sh.radixpoint, st_ms);
524524
}
525525
#if _lib_getrusage

src/cmd/ksh93/include/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
2525
#define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */
26-
#define SH_RELEASE_DATE "2022-06-09" /* must be in this format for $((.sh.version)) */
26+
#define SH_RELEASE_DATE "2022-06-12" /* must be in this format for $((.sh.version)) */
2727
#define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK
2828

2929
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */

src/cmd/ksh93/sh.1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,9 @@ hours if greater than zero,
22962296
minutes, and seconds of the form \fIHH\fPh\fIMM\fPm\fISS\fP.\fIFF\fPs.
22972297
The value of \fIp\fP determines whether or not the fraction is
22982298
included.
2299+
Seconds are zero-padded unless the
2300+
.B posix
2301+
shell option is on.
22992302
.IP
23002303
All other characters are output without change and a trailing
23012304
newline is added.
@@ -7747,6 +7750,12 @@ enables the recognition of a leading zero as introducing an octal number in
77477750
all arithmetic evaluation contexts, except in the \fBlet\fR built-in while
77487751
\fBletoctal\fR is off;
77497752
.IP \[bu]
7753+
disables zero-padding of seconds in the output of the
7754+
.B time
7755+
and
7756+
.B times
7757+
built-ins;
7758+
.IP \[bu]
77507759
stops the \fB.\fR command (but not \fBsource\fR) from looking up functions
77517760
defined with the \fBfunction\fR syntax;
77527761
.IP \[bu]
@@ -7987,6 +7996,9 @@ or
79877996
Displays the accumulated user and system CPU times, one line with the times
79887997
used by the shell and another with those used by all of the shell's child
79897998
processes. No options are supported.
7999+
Seconds are zero-padded unless the
8000+
.B posix
8001+
shell option is on.
79908002
.TP
79918003
\(dg \f3trap\fP \*(OK \f3\-p\fP \*(CK \*(OK \f2action\^\fP \*(CK \*(OK \f2sig\^\fP \*(CK .\|.\|.
79928004
The

src/cmd/ksh93/sh/xec.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ static void l_time(Sfio_t *outfile,register clock_t t,int precision)
239239
if(hr)
240240
sfprintf(outfile,"%dh",hr);
241241
if(precision)
242-
sfprintf(outfile,"%dm%02d%c%0*ds",min,sec,sh.radixpoint,precision,frac);
242+
sfprintf(outfile, sh_isoption(SH_POSIX) ? "%dm%d%c%0*ds" : "%dm%02d%c%0*ds", min, sec, sh.radixpoint, precision, frac);
243243
else
244-
sfprintf(outfile,"%dm%02ds",min,sec);
244+
sfprintf(outfile, sh_isoption(SH_POSIX) ? "%dm%ds" : "%dm%02ds", min, sec);
245245
}
246246

247247
#define TM_REAL_IDX 0
@@ -2401,11 +2401,9 @@ int sh_exec(register const Shnode_t *t, int flags)
24012401

24022402
if(t->par.partre)
24032403
{
2404-
Namval_t *np = nv_open("TIMEFORMAT",sh.var_tree,NV_NOADD);
2405-
if(np)
2404+
Namval_t *np;
2405+
if(np = nv_open("TIMEFORMAT",sh.var_tree,NV_NOADD))
24062406
format = nv_getval(np);
2407-
if(!format)
2408-
format = e_timeformat;
24092407
}
24102408
else
24112409
format = strchr(format+1,'\n')+1;

src/cmd/ksh93/tests/builtins.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,11 @@ then
746746
fi
747747
748748
# ======
749-
# 'times' builtin
749+
# 'time' keyword and 'times' builtin
750750
751-
expect=$'0m00.0[0-9][0-9]s 0m00.0[0-9][0-9]s\n0m00.000s 0m00.000s'
752-
actual=$("$SHELL" -c times)
753-
[[ $actual == $expect ]] || err_exit "times output: expected $(printf %q "$expect"), got $(printf %q "$actual")"
751+
exp=$'^user\t0m00.[0-9]{2}s\nsys\t0m00.[0-9]{2}s\n0m00.[0-9]{3}s 0m00.[0-9]{3}s\n0m00.000s 0m00.000s$'
752+
got=$("$SHELL" -c '{ time; } 2>&1; times')
753+
[[ $got =~ $exp ]] || err_exit "times output: expected match of $(printf %q "$exp"), got $(printf %q "$got")"
754754
755755
expect=$'*: times: too many operands'
756756
actual=$(set +x; eval 'times Extra Args' 2>&1)

src/cmd/ksh93/tests/posix.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ let "017 == 15" || err_exit "leading octal zero not recognised in 'let' in --pos
197197
(set --noposix --letoctal; let "017 == 15") || err_exit "leading octal zero not recognised in --noposix --letoctal (1)"
198198
(set --noposix; set --letoctal; let "017 == 15") || err_exit "leading octal zero not recognised in --noposix --letoctal (2)"
199199
200+
# disables zero-padding of seconds in the output of the time and times built-ins;
201+
exp=$'^user\t0m0.[0-9]{2}s\nsys\t0m0.[0-9]{2}s\n0m0.[0-9]{3}s 0m0.[0-9]{3}s\n0m0.000s 0m0.000s$'
202+
got=$("$SHELL" --posix -c '{ time; } 2>&1; times')
203+
[[ $got =~ $exp ]] || err_exit "POSIX time/times output: expected match of $(printf %q "$exp"), got $(printf %q "$got")"
204+
200205
# stops the . command (but not source) from looking up functions defined with the function syntax;
201206
echo 'echo SCRIPT' >scrunction
202207
function scrunction { echo FUNCTION; }

0 commit comments

Comments
 (0)