Shell – ksh – subtract 5 minutes from current time

kshscriptingshell-scripttimestamps

I need to get the date and time of the command date minus 5 mins.

date:
29 Aug 2018 21:56:01

result:
29 Aug 2018 21:51:02

I tried to search on the Internet/forums but most of it involved using the -d option and what I have doesn't support the -d option.

This one works on Bash but I need something that works on ksh.

$ printf "%(%d %h %Y %H:%M:%S)T\n" $(( $(printf "%(%s)T") - 5 * 60 ))

Best Answer

You could use perl:

echo $(( $(printf "%(%s)T") - 5 * 60 )) | perl -lne 'print scalar localtime $_'

Or in pure perl:

perl -le 'print scalar localtime(time()-5*60)'

To format use strftime():

perl -MPOSIX=strftime -le 'print strftime("%d %h %Y %H:%M:%S", localtime(time()-5*60))'
Related Question