11 ms·
You can also skip the subshell invocation of date by using %(fmt)T from bash's printf: %(fmt)T -output the date-time string resulting from using FMT as a for
by bmoyles 1y ago
You can also skip the subshell invocation of date by using %(fmt)T from bash's printf:
%(fmt)T -output the date-time string resulting from using FMT as a format string for strftime(3)
The man page provides a bit more detail:
%(datefmt)T causes printf to output the date-time string resulting from using datefmt as a format string for strftime(3). The corresponding argument is an integer
representing the number of seconds since the epoch. Two special argument values may be used: -1 represents the current time, and -2 represents the time
the shell was invoked. If no argument is specified, conversion behaves as if -1 had been given. This is an exception to the usual printf behavior.
With that,
timestamp=$(date +'%y.%m.%d %H:%M:%S')
becomes
printf -v timestamp '%(%y.%m.%d %H:%M:%S)T' -1
- gkfasdfasdf 1y agoThat would indeed be faster, looks like it requires bash 4.2+