How to calculate time difference between start and end time of a process!

Hello All,

I have a problem calculating the time difference between start and end timings...!

the timings are given by 24hr format..

Start Date : 08/05/10 12:55
End Date : 08/09/10 06:50

above values are in mm/dd/yy hh:mm format.

Now the thing is, 7th(08/07/10) and 8th (08/08/10) of August are saturday and Sunday. So I dont want those timings( i mean saturday and sunday- script should check for weekends and should not count the timings for weekends). Finally i need the output in hr format which is the difference between start and end timings excluding weekends.

can anybody help me in this.

Thank you.

#!/bin/bash
StartDate="08/05/10 12:55"
EndDate="08/09/10 06:50"
HRS=$((($(date -d "$EndDate" +%s)-$(date -d "$StartDate" +%s))/3600))
WHRS=$HRS
for (( D=1; D<$((HRS/24+1)); D++ ))
do
   [ $(date -d "$StartDate $D days" +%u) -ge 6 ] && ((WHRS-=24))
done
echo "Working time elapsed='$WHRS'"

As a function

#!/bin/bash
Elapsed()	{
   HRS=$((($(date -d "$2" +%s)-$(date -d "$1" +%s))/3600))
   WHRS=$HRS
   for (( D=1; D<$((HRS/24+1)); D++ ))
   do
      [ $(date -d "$1 $D days" +%u) -ge 6 ] && ((WHRS-=24))
   done
   echo $WHRS
}
StartDate="08/05/10 12:55"
EndDate="08/09/10 06:50"
echo "Working time elapsed='$(Elapsed "$StartDate" "$EndDate")'"

frans,

Thanks for your reply.

But its not working for me.. I am not that good at shell scripting.. I am a beginner in this.

at first when i run that function script i got the below error..

interpreter "usr/bin/bash" not found
interpreter "usr/bin/bash" not found
interpreter "usr/bin/bash" not found

later i deleted the first line i,e #!/bin/bash and then it gave the below error

syntax error at line 4 : `((' unexpected

pls tell me where i went wrong.

Thank you,.

---------- Post updated at 10:06 AM ---------- Previous update was at 10:04 AM ----------

and also for date -d it'll give illegal option -d

What OS are you running ?

Do you need it in HHMMSS format?

If not, you can check the environment variable called: ${SECONDS}

startTime=${SECONDS}
# Do some stuff here
endTime=${SECONDS}
diffTime=`expr ${endTime} - ${startTime}`
echo "Diff Time: [${diffTime}]"

time {application name goes here}

the real time is the time that the process was running

I do not have GNU date. So I got frustrated with limited options to get elapsed time in Unix,, I wrote a method in Java and using it..
If this works I could replace java method with this..

diffTime=`expr ${endTime} - ${startTime}`

Does this approach work if a main program is calling say 1. common functions included from other ksh programs and 2. included java programs(from jar/class) ? I know PID is same thrughout the execution of my program.

In other words can we use this to reliably find elapsed time to execute complex Admin scripts in Unix ?

say my environment variable is this

: once I execute abc.sh Does it start incrementing SECONDS as soon as any PID starts in the runtime and ends incrimenting before that PID exits from shell !?

Here's a Perl program that takes care of the requirement to exclude weekends while determining the difference between two dates.

$
$
$ cat -n timediff.pl
     1  #perl -w
     2  use Date::Calc qw(:all);
     3  if ($#ARGV != 1) {
     4    print "Usage: perl timediff.pl <START_DATE> <END_DATE>\n";
     5    print "Both dates in \"mm/dd/yyyy hh24:mi:ss\" format\n";
     6    exit;
     7  }
     8  # @sd = ($y,$mm,$d,$h,$mi,$s); same as @ed
     9  @sd = (split/[ :\/]/,$ARGV[0])[2,0,1,3,4,5];
    10  $sd[5] eq "" and $sd[5]=0;
    11  $sdow = Day_of_Week(@sd[0,1,2]);
    12  printf ("START DATE => %s, %d/%d/%d %02d:%02d:%02d\n",
    13           Day_of_Week_Abbreviation($sdow),@sd[1,2,0,3,4,5]);
    14  @ed = (split/[ :\/]/,$ARGV[1])[2,0,1,3,4,5];
    15  $ed[5] eq "" and $ed[5]=0;
    16  $edow = Day_of_Week(@ed[0,1,2]);
    17  printf ("END DATE   => %s, %d/%d/%d %02d:%02d:%02d\n",
    18           Day_of_Week_Abbreviation($edow),@ed[1,2,0,3,4,5]);
    19  # === Dates must be in chronological order ===
    20  if (Date_to_Time(@sd) > Date_to_Time(@ed)) {
    21    print "The dates are not in chronological order. Abnormal Exit.\n";
    22    exit;
    23  }
    24  # === Case 1 : Both dates lie within the weekend ===
    25  $ddelta = Delta_Days(@sd[0,1,2], @ed[0,1,2]);
    26  # Dow = 1 for Monday; Dow = 7 for Sunday
    27  if ($sdow > 5 and $edow > 5 and $ddelta <= 1) {
    28    print "Both dates lie within the weekend. Elapsed time will not be calculated.\n";
    29    exit;
    30  }
    31  # === Case 2 : The dates do not span more than one day ===
    32  if ($ddelta == 0) {
    33    ($dd,$dh,$dm,$ds) = Delta_DHMS(@sd, @ed);
    34    printf("Elapsed time = %d days %d hours %d minutes %d seconds\n", $dd,$dh,$dm,$ds);
    35    exit;
    36  }
    37  # === Case 3 : The dates span multiple days; we'll loop through them and exclude weekends ===
    38  for ($i = 0; $i <= $ddelta; $i++) {
    39    @d = Add_Delta_Days(@sd[0,1,2],$i);
    40    $ddow = Day_of_Week(@d[0,1,2]);
    41    if ($i == 0 and $ddow <= 5) {
    42      @next = Add_Delta_Days(@sd[0,1,2],1);
    43      ($dd,$dh,$dm,$ds) = Delta_DHMS(@sd, @next,0,0,0);
    44    } elsif ($i == $ddelta and $ddow <= 5) {
    45      @y = Delta_DHMS(@ed[0,1,2],0,0,0, @ed);
    46      $dd += $y[0]; $dh += $y[1];
    47      $dm += $y[2]; $ds += $y[3];
    48    } elsif ($ddow <= 5) {
    49      $ds += 24*60*60;
    50    }
    51  }
    52  @normalized = Normalize_DHMS($dd,$dh,$dm,$ds);
    53  printf("Elapsed time, excluding weekends = %d days %d hours %d minutes %d seconds\n", @normalized);
$
$
$ # Incorrect invocation
$ perl timediff.pl
Usage: perl timediff.pl <START_DATE> <END_DATE>
Both dates in "mm/dd/yyyy hh24:mi:ss" format
$
$ # Dates in reverse order
$ perl timediff.pl "1/2/2010 10:11:12" "1/1/2010 15:16:17"
START DATE => Sat, 1/2/2010 10:11:12
END DATE   => Fri, 1/1/2010 15:16:17
The dates are not in chronological order. Abnormal Exit.
$
$ # Dates lying within the weekend
$ perl timediff.pl "1/2/2010 0:0:0" "1/3/2010 23:59:59"
START DATE => Sat, 1/2/2010 00:00:00
END DATE   => Sun, 1/3/2010 23:59:59
Both dates lie within the weekend. Elapsed time will not be calculated.
$
$ # And the rest of the testcases...
$ perl timediff.pl "1/1/2010 0:0:0" "1/1/2010 23:59:59"
START DATE => Fri, 1/1/2010 00:00:00
END DATE   => Fri, 1/1/2010 23:59:59
Elapsed time = 0 days 23 hours 59 minutes 59 seconds
$
$ perl timediff.pl "1/1/2010 0:0:0" "1/2/2010 0:0:0"
START DATE => Fri, 1/1/2010 00:00:00
END DATE   => Sat, 1/2/2010 00:00:00
Elapsed time, excluding weekends = 1 days 0 hours 0 minutes 0 seconds
$
$
$ # From a weekend date to a non-weekend date
$ perl timediff.pl "1/2/2010 11:12:13" "1/5/2010 14:15:16"
START DATE => Sat, 1/2/2010 11:12:13
END DATE   => Tue, 1/5/2010 14:15:16
Elapsed time, excluding weekends = 1 days 14 hours 15 minutes 16 seconds
$
$
$ # From a non-weekend date to a weekend date
$ perl timediff.pl "1/1/2010 11:12:13" "1/3/2010 14:15:16"
START DATE => Fri, 1/1/2010 11:12:13
END DATE   => Sun, 1/3/2010 14:15:16
Elapsed time, excluding weekends = 0 days 12 hours 47 minutes 47 seconds
$
$
$ # Encompassing one weekend
$ perl timediff.pl "1/1/2010 11:12:13" "1/4/2010 14:15:16"
START DATE => Fri, 1/1/2010 11:12:13
END DATE   => Mon, 1/4/2010 14:15:16
Elapsed time, excluding weekends = 1 days 3 hours 3 minutes 3 seconds
$
$
$ # Encompassing more than one weekend
$ perl timediff.pl "1/1/2010 11:12:13" "1/14/2010 14:15:16"
START DATE => Fri, 1/1/2010 11:12:13
END DATE   => Thu, 1/14/2010 14:15:16
Elapsed time, excluding weekends = 9 days 3 hours 3 minutes 3 seconds
$
$
$ # An entire year
$ perl timediff.pl "1/1/2010 0:0:0" "12/31/2010 23:59:59"
START DATE => Fri, 1/1/2010 00:00:00
END DATE   => Fri, 12/31/2010 23:59:59
Elapsed time, excluding weekends = 260 days 23 hours 59 minutes 59 seconds
$
$
$

HTH,
tyler_durden

1 Like

Hi all,

Thank you very much for all your reply's.

I am using unix HP-UX machine for this..

can i use the above perl script in HP UX m/c? do i need to do any modification?

If your system has Perl and the Date::Calc module installed in it, then you should be able to use the script.

Not that I know of.

tyler_durden

this is what the error i got when i tried to test the above perl script

perl timediff.pl "1/2/2010 10:11:12" "1/1/2010 15:16:17"
Can't locate Date/Calc.pm in @INC (@INC contains: /opt/perl/lib/5.8.3/PA-RISC1.1-thread-multi /opt/perl/lib/5.8.3 /opt/perl/lib/site_perl/5.8.3/PA-RISC1.1-thread-multi /opt/perl/lib/site_perl/5.8.3 /opt/perl/lib/site_perl .) at timediff.pl line 2.
BEGIN failed--compilation aborted at timediff.pl line 2.

---------- Post updated at 07:18 AM ---------- Previous update was at 07:17 AM ----------

oh thats what the error i am getting :frowning: DATE/calc.pm not found :frowning: is there anything i can do for that? i cannot install anything because its office system :frowning:

can that perl program work with timezone switching?

Say we are suppoesd to change our clocks back @2:00 AM to 1:00 AM
sd="mm/dd/yy 01:55:00 AM EDT' and ed='mm/dd/yy 01:10:00 AM EDT'

Does your perl program give elapsed time as 1hr:15 minutes?

Yes, you can. Install the Date::Calc module.

tyler_durden

I cant do that since it is office system :frowning: any other alternative?

The way it is now - it doesn't work with timezone switching.

No.

tyler_durden

---------- Post updated at 12:08 PM ---------- Previous update was at 12:08 PM ----------

No.

tyler_durden

can i run that script in windows? if yes can you give me the link to download perl for windows having DATE::Calc command please

---------- Post updated at 09:15 AM ---------- Previous update was at 08:59 AM ----------

and also while googling i found out that i just need to run something to install Date::calc in unix. is that true? if yes then can u give me that file which i need to run, if you have it with you?

Yes, you can, as long as you have Perl and the Date::Calc module installed in your system.

Date::Calc is a Perl module and not a "command". Try ActiveState Perl; it's a popular Windows port of Perl.

http://www.activestate.com

Use PPM (Perl Package Manager) that ships with their Perl port to install Date::Calc from CPAN.

It's not a "file". You can install Perl modules in many different ways in Linux (not sure about Unix). Have a look at perlmodinstall.

http://theoryx5.uwinnipeg.ca/CPAN/perl/pod/perlmodinstall.html

HTH,
tyler_durden