Solved

Calculating Elapsed Time in Bash Shell on Oracle Linux 6.5

Posted on 2014-04-12
3
675 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

729 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