Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calculating Elapsed Time in Bash Shell on Oracle Linux 6.5

Posted on 2014-04-12
3
Medium Priority
?
716 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 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

636 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