November 25, 2013 Archives

25-11-2013 18:34

Time calculations in shell

I have had needs where I needed to calculate the time something has taken to run. It can be hard when going over a date in the calendar. To add to that, some shells don't allow floating point calculation too. So I sat down and worked out a scrpt that when given a start second and a finish second (like seconds since epoch), it can show the difference in HOURS:MINUTES. Feel free to modify as you please, GPL3 of course.

#!/bin/bash

# Capture the epoch start and then finish time.
#SLEEPSTOP=1385183548
#SLEEPSTART=1385180248

# Or give it the absolute seconds.
SLEEPSTOP=0
SLEEPSTART=1440

SLEEP=$(($SLEEPSTOP-$SLEEPSTART))
HR=$(($SLEEP/60/60))
MINS=$(echo $(($SLEEP/60*100/60))|rev|cut -c-2|rev)
MINS=$((MINS*60/100))

echo ${HR}:${MINS}


Posted by DaveQB | Permanent Link | Categories: IT