The Shell Scripting Tutorial


Hours, Minutes, Seconds

Formatting and Displaying Time in Hours, Minutes and Seconds

9 May 2016

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
# https://www.shellscript.sh/examples/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 H_TAG M_TAG S_TAG
  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
    # Using the x_TAG variables makes this easier to translate; simply appending
    # "s" to the word is not easy to translate into other languages.
    [ "$H" -eq "1" ] && H_TAG="hour" || H_TAG="hours"
    [ "$M" -eq "1" ] && M_TAG="minute" || M_TAG="minutes"
    [ "$S" -eq "1" ] && S_TAG="second" || S_TAG="seconds"
    [ "$H" -gt "0" ] && printf "%d %s " $H "${H_TAG},"
    [ "$SECONDS" -ge "60" ] && printf "%d %s " $M "${M_TAG} and"
    printf "%d %s\n" $S "${S_TAG}"
  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:

123 seconds = 2m3s

An example of running the script, with a sleep of 11723 seconds (three hours, fifteen minutes and twenty-three seconds), looks like this:

11723 seconds = 3h15m23s

See Also

See also: UNIX Epoch 1970


My Paperbacks and eBooks

My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.

Shell Scripting Tutorial

Shell Scripting Tutorial
is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion.

Also available in PDF form from Gumroad:Get this tutorial as a PDF
Shell Scripting: Expert Recipes for Linux, Bash and more

Shell Scripting: Expert Recipes for Linux, Bash and more
is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script.