I need to somehow use the date command in bash or another utility to print out the date and time, 5 minutes before and 5 minutes after a given value. For example:
i
With GNU date
, you can do a simple form of date/time arithmetic with the argument to the --date
option:
$ date --date 'now + 5 minutes'
With BSD date
(at least, the version that ships with Mac OS X), the -v
option allows you to do something similar, but only with the current time:
$ date -v +5M
$ date -v -5M
Otherwise, you need to perform the math explicitly after converting the time to a UNIX timestamp (seconds since Jan 1, 1970):
$ date -r $(( $(date +%s) + 300 )) # 5 minutes (300 seconds) from now
$ date -r $(( $(date +%s) - 300 )) # 5 minutes ago