To get difference between 2 times.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To get difference between 2 times.
# 1  
Old 04-05-2012
To get difference between 2 times.

Can anyone help me to write a script to get diffrence between 2 times. (platform- solaris)

i.e., Diffence shoud be displayed in tems of hours. (Approximately)

for eg:
file1 = 05-Apr-2012 13:42:32
file2 = 04-Apr-2012 12:42:41

O/P is like :
diff = 25 Hrs
# 2  
Old 04-05-2012
Do you have the GNU stat or date command on your system?
# 3  
Old 04-05-2012
Perl, ok?

Code:
#! /usr/bin/perl -w
use strict;
use POSIX 'strftime';

my $file1 = "05-Apr-2012 13:42:32";
my $file2 = "04-Apr-2012 12:42:41";

my $diff_hrs = ((parse_date ($file1) - parse_date ($file2)) / 3600);
print "$diff_hrs\n";

sub parse_date {
    my $t = shift;
    my %mnths = (   "Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06",
                    "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12" );
    my $dt = substr ($t, 0, 2);
    my $mt = substr ($t, 3, 3);
    my $yr = substr ($t, 7, 4) - 1900;
    my $hr = substr ($t, 12, 2);
    my $mn = substr ($t, 15, 2);
    my $sc = substr ($t, 18, 2);
    
    $mt = $mnths{"$mt"} - 1;
    return strftime ("%s", $sc, $mn, $hr, $dt, $mt, $yr);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

3. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

4. Shell Programming and Scripting

Listing times from ls

Hello, Im new to shell scripting and i am trying to write a part of my script that will search for all files in any given folder and write down all the names of the files and the atime, change time, and modtime of the files in one file as an output. I know that ls -l, ls -ul and ls -lc will give... (1 Reply)
Discussion started by: jnagda
1 Replies

5. Shell Programming and Scripting

Getting logs between two times

Hi, I need to read log file for the period between two time frame.eg all those logs between 3AM to 4M. Any command to use for the shell scripting? Ahamed (5 Replies)
Discussion started by: ahamed
5 Replies

6. UNIX for Dummies Questions & Answers

comparing two times

Hi Good Morning all, I have two dates and times in the beloe format. 1. 07/18/2009 03:45:49 2. 07/18/2009 03:46:41 i will grep these values from a file. Now my problem is i need to find out whether the second valus is bigger thatn the 1 value. How to compare the times. Please help me in... (4 Replies)
Discussion started by: intiraju
4 Replies

7. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies

8. Shell Programming and Scripting

Question about times and so

I've got an logfile like: Start dump db_name1 : Wed Oct 30 15:08:47 MET 2002 Ready dump db_name1 : Wed Oct 30 15:14:46 MET 2002 Start dump db_name2 : Wed Oct 30 15:14:46 MET 2002 Ready dump db_name2 : Wed Oct 30 15:15:36 MET 2002 Start dump db_name3 : Wed Oct 30 15:15:36 MET 2002... (1 Reply)
Discussion started by: TheBlueLady
1 Replies
Login or Register to Ask a Question