?
Solved

Calculating Elapsed Time in Bash Shell on Oracle Linux 6.5

Posted on 2014-04-12
3
Medium Priority
?
738 Views
Last Modified: 2014-04-17
I want to be able to take calulate milliseconds by taking an input and then returning elapsed time as HH:MM:SS.NNN

I was taking this code (http://www.linuxjournal.com/content/use-date-command-measure-elapsed-time) as my basis and trying to expand to include milliseconds...but I am getting into a right old mess with the math

The original code works fine:
#!/bin/bash

function timer()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s')
    else
        local  stime=$1
        etime=$(date '+%s')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        ds=$((dt % 60))
        dm=$(((dt / 60) % 60))
        dh=$((dt / 3600))
        printf '%d:%02d:%02d' $dh $dm $ds
    fi
}

#test the function
start=$(timer)
sleep 5
stop=$(timer $start)
echo "Elapsed time: $stop " 

Elapsed time: 0:00:05

Open in new window


Now I want to take the input to be milliseconds (rather than seconds) and return HH:MM:SS.NNN.  I did this but it fails horribly:

#!/bin/bash

function timer2()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s%N')

    else
        local  stime=$1
        etime=$(date '+%s%N')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        dn=  ## QUESTION:  how do I set the milliseconds to 3 places (not 9)
        ds=$(((dt % (1000*60*60)) % (1000*60)) / 1000)
        dm=$(((dt % (1000*60*60)) / (1000*60)  ))
        dh=$(( dt / (1000*60*60)))
        printf '%d:%02d:%02d.%03d' $dh $dm $ds $dn
    fi
}

# test function
start=$(timer2)
sleep 2
stop=$(timer2 $start)
echo "Elapsed time: $stop " 

Open in new window


How do I set dn for the milliseconds and what is the correct math to get desired out put?

Any help would be greatly appreciated.
0
Comment
Question by:klyles95
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1000 total points
ID: 39996833
%N means nanoseconds (1 sec = 1000000000 nanoseconds).
So you'll have to use 1000000000 or (1000*1000*1000) instead of just 1000.

To get milliseconds you must perform an integer division by (1000*1000) after calculating the remainder of the division by (1000*1000*1000)

This should work: (the script expects the "sleep" seconds as a command line parameter):

#!/bin/bash
function timer2()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s%N')

    else
        local  stime=$1
        etime=$(date '+%s%N')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        dn=$((dt % (1000*1000*1000)/(1000*1000)))
        ds=$((dt / (1000*1000*1000)%(60*60)%60))
        dm=$((dt / (1000*1000*1000)%(60*60)/60))
        dh=$((dt / (1000*1000*1000)/(60*60)))
        printf '%d:%02d:%02d.%03d' $dh $dm $ds $dn
    fi
}

# test function
start=$(timer2)
sleep $1
stop=$(timer2 $start)
echo "Elapsed time: $stop "

Open in new window

I have to leave now, sorry! If I made a mistake (I probably did) let's talk about it tomorrow!

EDIT: If you want to half-adjust the miliseconds then calculate "dn" like this:

dn=$(((dt+500000) % (1000*1000*1000)/(1000*1000)))

wmp
0
 

Author Comment

by:klyles95
ID: 40007975
Thank you wmp - this didnt quite  work.  I was getting the same result returned.  

I ended up doing this calculation differently by separating the milliseconds from the seconds and doing the math that way.

Thank you for responding
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40007996
You could have asked me to improve my work instead of giving a grade C straightaway.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question