Skip to content

Commit

Permalink
[builtin/printf] Support width and precision for %()T
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 19, 2020
1 parent 28c169f commit 1c84df0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion osh/builtin_printf.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _ParseFormatStr(self):
p_die(msg, token=self.cur_token)

# Do this check AFTER the floating point checks
if part.precision and part.type.val not in 'fs':
if part.precision and part.type.val[-1] not in 'fsT':
p_die("precision can't be specified when here",
token=part.precision)

Expand Down Expand Up @@ -329,6 +329,8 @@ def Run(self, cmd_val):
elif d == -2: # shell start time
d = shell_start_time
s = time.strftime(typ[1:-2], time.localtime(d));
if precision is not None:
s = s[:precision] # truncate

else:
raise AssertionError()
Expand Down
16 changes: 16 additions & 0 deletions spec/builtin-printf.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,19 @@ status=0
## N-I dash/mksh/zsh/ash STDOUT:
status=1
## END

#### %10.5(strftime format)T
# The result depends on timezone
export TZ=Asia/Tokyo
printf '[%10.5(%Y-%m-%d)T]\n' 1557978599
export TZ=US/Eastern
printf '[%10.5(%Y-%m-%d)T]\n' 1557978599
echo status=$?
## STDOUT:
[ 2019-]
[ 2019-]
status=0
## END
## N-I dash/mksh/zsh/ash STDOUT:
[[status=1
## END

0 comments on commit 1c84df0

Please sign in to comment.