?
Solved

Calculating Elapsed Time in Bash Shell on Oracle Linux 6.5

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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

764 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