1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Calculating Time Differences in a Batch Script

Discussion in 'Software Development' started by lancastert, Oct 10, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. lancastert

    lancastert Thread Starter

    Joined:
    Apr 5, 2007
    Messages:
    9
    I am having a problem calculating time differences in batch.

    I want my script to work out when a user started a process and when they finished it and calulate how long it took them.

    E.g. Time In: 09:02. Time out: 11:52 which works out at 02:50 or 2 Hours 50 Mins.

    Does any one know how can I set this up in a Batch script?
     
  2. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    You can use the date command (just before the user process is executed) in a script and assign it to a variable, then parse the variable with a nawk script to get the start time. And just after the user process is finished executing in the script, you can repeat the above sequence to get the finish time and then pick off the difference in hours, minutes and seconds by using expr in the bach script.

    -- Tom
     
  3. lancastert

    lancastert Thread Starter

    Joined:
    Apr 5, 2007
    Messages:
    9
    Can you show me an example please?

    I was also thinking down this route:

    Can I use the SLEEP command to count upto an hour then add 1 to a variable for the hour and so forth until the user presses a certain key which will prompt the script to add a minute value to another variable? Then I can combine the two variables in this format HH:MM
     
  4. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi lancastert,

    I would not use that sleep command as you described.

    To store the date in a the variable named date:
    date=`date`

    Note, the hh:mm:ss are in the 4th item in the output of the date command, or:
    echo $date

    To capture capture and print the hh:mm:ss at the start of the script into a variable named stime:
    date=`date`
    stime=`echo $date | nawk '{print $4}'`
    echo $stime

    Simlarly, at the end of the user process execution in the script, you can capture the end time with:
    date=`date`
    etime=`echo $date | nawk '{print $4}'`
    echo $etime

    To bring it all together since you now know how to capture the start and end time, you need to use nawk with a separator field, ":" in order to separate the hh:mm:ss, and then use the expr facility of the script language (sh, bash) to compute the difference in hours and seconds.

    Search for a Shell tutorial and use of expr on the Internet for more information by trying:
    "Shell tutorial" +expr

    or figure it out by this example if you can (see the man page on expr:

    #!/bin/sh
    i=1
    while [ $i -le 5 ] ; do
    echo $i
    i=`expr $i +1` # add one to i
    done

    -- Tom
     
  5. devil_himself

    devil_himself

    Joined:
    Apr 7, 2007
    Messages:
    4,910
    here a pure ms dos batch script

    Code:
    ::save as "timediff.bat"
    ::usage --> timediff hr1 mn1 sc1 hr2 mn2 sc2
    ::ex --> timediff 12 23 40 20 9 12
    @echo off
    
    :: convert input hours to seconds
    set /a hh1=60*60*%1
    set /a hh2=60*60*%4
    
    :: convert input minutes to seconds
    set /a mm1=60*%2
    set /a mm2=60*%5
    
    :: input seconds
    set /a ss1=%3
    set /a ss2=%6
    
    :: add input hours , minutes , seconds 
    set /a t1=hh1+mm1+ss1
    set /a t2=hh2+mm2+ss2
    
    :: calculate difference in seconds
    set /a ds=t2-t1
      
    if %ds% LSS 0 set /a ds=%ds%+24*60*60
    
    set /a h=(%ds%/3600)
    set /a m=(%ds%/60)-60*%h%
    set /a s=%ds%-60*(%ds%/60)
    
    echo hrs=%h% min=%m% sec=%s%
    goto :EOF
    
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/757804

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice