9th May 2016
Calculating Hours, Minutes and Seconds
Sometimes a script will take a varying amount of time to run, and you want it to report how long it took. Sure, you can say when it started and finished, but that's not particularly helpful. The GNU Date command has the "date +%s
" option, which gives you the number of seconds since 1st January, 1970. By noting that value before and after running the script, you can get a nice simple integer telling you how many seconds the script took to run.
So you can display a message saying "That took 11723 seconds to run", for example. It would be nicer to say "That took 3 hours, 15 minutes and 23 seconds to run", though.
This function uses Bash's simple built-in integer mathematical capabilities to display a duration (in seconds) in a more human-readable form. For completeness, it actually offers two different formats; by default, it displays "03h15m23s", but if you call it with the word "long" as the second argument, it will say "3 hours, 15 minutes and 23 seconds".
These little touches make a script just that bit more friendly and useful.
#!/bin/bash # http://steve-parker.org/sh/tips/hms/ # Example of Hours, Minutes, Seconds. # Look for the "*** To avoid having to wait" comments # for a quicker way of playing with this! # Make a note of the start time (in seconds since 1970) STARTTIME=`date +%s` echo "`date`: Starting the script." # Define hms() function hms() { # Convert Seconds to Hours, Minutes, Seconds # Optional second argument of "long" makes it display # the longer format, otherwise short format. local SECONDS H M S MM SECONDS=${1:-0} let S=${SECONDS}%60 let MM=${SECONDS}/60 # Total number of minutes let M=${MM}%60 let H=${MM}/60 if [ "$2" == "long" ]; then # Display "1 hour, 2 minutes and 3 seconds" format [ "$H" -gt "0" ] && printf "%d %s " $H "hours," [ "$SECONDS" -ge "60" ] && printf "%d %s " $M "minutes and" printf "%d %s\n" $S "seconds" else # Display "01h02m03s" format [ "$H" -gt "0" ] && printf "%02d%s" $H "h" [ "$M" -gt "0" ] && printf "%02d%s" $M "m" printf "%02d%s\n" $S "s" fi } ############################################################ # Main script - could be anything, but takes a # non-predictable amount of time. # Sleep for 123 seconds (2m 3s) unless some other value # is passed to the script. # *** To avoid having to wait, comment out the following line: sleep ${1:-123} ############################################################ # At end of script, report how long it took. # Make a note of the end time (in seconds since 1970) echo "`date`: The script has finished." ENDTIME=`date +%s` # Calculate the difference - that's how long the script took to run let DURATION=${ENDTIME}-${STARTTIME} # *** To avoid having to wait, uncomment the following line: #DURATION=$1 HOWLONG=`hms $DURATION` echo "That took ${HOWLONG} to run (short format)" HOWLONG=`hms $DURATION long` echo "That took ${HOWLONG} to run (long format)"Download the hms.sh script
An example of running the script, with a sleep
of 123 seconds (two minutes and three seconds), looks like this:
An example of running the script, with a sleep
of 11723 seconds (three hours, fifteen minutes and twenty-three seconds), looks like this:
Invest in your career. Buy my Shell Scripting Tutorial today:
og:image credit: © https://www.sparkfun.com/tutorials/47