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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to calculate time difference between start and end time of a process!
# 8  
Old 09-03-2010
Here's a Perl program that takes care of the requirement to exclude weekends while determining the difference between two dates.

Code:
$
$
$ 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
This User Gave Thanks to durden_tyler For This Post:
# 9  
Old 09-03-2010
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?
# 10  
Old 09-03-2010
Quote:
Originally Posted by smarty86
...
can i use the above perl script in HP UX m/c?
If your system has Perl and the Date::Calc module installed in it, then you should be able to use the script.

Quote:
do i need to do any modification?
Not that I know of.

tyler_durden
# 11  
Old 09-03-2010
this is what the error i got when i tried to test the above perl script

Code:
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 Smilie DATE/calc.pm not found Smilie is there anything i can do for that? i cannot install anything because its office system Smilie
# 12  
Old 09-03-2010
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?
# 13  
Old 09-03-2010
Quote:
Originally Posted by smarty86
... is there anything i can do for that? ...
Yes, you can. Install the Date::Calc module.

tyler_durden
# 14  
Old 09-03-2010
I cant do that since it is office system Smilie any other alternative?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate time difference between two lines

i grepped the time stamp in a file as given below now i need to calculate time difference file data: 18:29:10 22:15:50 (5 Replies)
Discussion started by: vivekn
5 Replies

2. Shell Programming and Scripting

Calculate time difference

I have time in a file in HH:MM:SS format as it contents(its not the file creation time). i need this to be converted to epoch time or time since 1970. The time is written into that file by a script, which i cannot modify. Im using AIX machine $ cat abc.txt 10:29:34 (2 Replies)
Discussion started by: gpk_newbie
2 Replies

3. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

4. Linux

Process start time not showing correct time

Process start time is not showing the correct time: I had started a process on Jun 17th at 23:30:00. Next day morning when I run the command "ps -ef | grep mq", the process is showing the start date of Jun 17th but the start time is 00:16:41 Day/Date is setup correctly on the server. It... (2 Replies)
Discussion started by: hemangjani
2 Replies

5. Shell Programming and Scripting

How to get data between the start time and end time?

Hi, Can anyone help me how can I get the line that between the start time and end time. file1.txt 15/03/2009 20:45:03 Request: - Data of this line 15/03/2009 20:45:12 Response: - Data of this line 15/03/2009 22:10:40 Request: - Data of this line 15/03/2009 22:10:42 Response: - Data of... (1 Reply)
Discussion started by: tanit
1 Replies

6. Shell Programming and Scripting

How to calculate the time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

7. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies

8. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies

9. Shell Programming and Scripting

Start time/end time and status of crontab job

Is there anyway to get the start time and end time / status of a crontab job which was just completed? Of course, we know the start time of the crontab job since we are scheduling. But I would like to know process start and time recorded somewhere or can be fetched from a command like 'ps'. ... (3 Replies)
Discussion started by: thambi
3 Replies
Login or Register to Ask a Question