Originally posted by: Wouter Liefting
I couldn't think of an easy way to do this with shell constructs and/or the date command only. However, in perl it's easy.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( time() - 5);
print "$hour:$min:$sec";
You can include perl code in the perl call with -e, so from the shell this would become:
perl -e ' ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( time() - 5); print "$hour:$min:$sec"; '
Obviously the "- 5" is the offset, and the print "$hour:$min:$sec" can be modified to generate any output you need.
Be aware that the $year is the current year minus 1900, and that $mon starts counting from 0. http://perldoc.perl.org/functions/localtime.html
If you are happy with the default scalar output of localtime ( for example Mon Mar 9 15:03:18 2015 ) you could even do this:
perl -e '$string = localtime( time - 5 ); print $string;'