raghvens has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks,

I am trying to write a script to get the date difference between the clearcase view created with the current date. I know that there is a module readily available in perl to get datediff but without using it, I want to write the script which will tell the view created in Years, Months and days. Please suggest.

@date = localtime(); $y = sprintf("%d %02d %02d\n",$date[5]+1900,$date[4]+1,$date[3]); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); $year += 1900; @MonthNames = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", + "Sep", "Oct", "Nov", "Dec"); chomp($y); #print "\nToday's Date ----> $y"; #--------------------------------------------------------------------- +--------- # Timestamp / get actual date, check for environment, configuration fi +les #--------------------------------------------------------------------- +--------- my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year += 1900; @date = localtime(); $year = sprintf("%s-%02d-%02d\n",$date[5]+1900,$date[4]+1,$date[3]); chomp($year); open(LOG, ">~veiwreport.txt") or die $!; #@vlist = "raghvens_test_view"; @vlist = `cleartool lsview -s`; foreach (@vlist) { chomp($_); #system "color 0A"; print "\nTag: $_"; print LOG "\nTag: $_"; @view_space = `cleartool space -view $_`; foreach $line (@view_space) { if ($line =~ m/is\s(.*)\sMb/) { #chomp($1); #$size=$1; #print " View-Size:$size Mb"; @size = split(/\s/,$line); #chomp($size[1]); print " Size : $size[7] Mb"; print LOG " Size : $size[7] Mb"; } } @view_details = `cleartool lsview -prop -full $_`; foreach $vd (@view_details) { if($vd =~ m/Last accessed/) { @arr = split(/\s+/,$vd); $accdate = $arr[2]; @acc = split(/T/,$accdate); chomp(@acc[0]); print "\tLast accessed : @acc[0]"; print LOG "\t Last accessed : @acc[0]"; } if ($vd =~ /Created/) { @a=split(".",$vd,1); #print "\nHellooooooo----> $a[0]"; @b=split("T",$a[0]); #@c=split("\w",$b[0]); $b[0] =~ s/(\w)*/ /; $b[0] =~ s/\s+//; print " created on $b[0]"; print LOG " created on $b[0]"; @c=split(/\-/,$b[0]); $d=@c[0]." ".@c[1]." ".@c[2]; #print "\nDate :: @c[2]"; #print "\nMonth:: @c[1]"; #print "\nYear :: @c[0]"; @ar=split("-",$d); $i=0; $i=sprintf("%2d",$i); foreach $month(@MonthNames) { $i++; if ($month eq $ar[1]) { $month=$i; if ($i <= 9) { $month= "0".$i; } $datee=$ar[2].$month.$ar[0]; #print "\nNEW DATE ::$datee \n"; } } #print "\n NEW DATE :: $datee \n"; $datee=$ar[2].$month.$ar[0]; #print"\n>>>>>>>>>>>>>>>$datee<<<<<<<<"; } } # print " >>>>>>>>>>>>> $d \n"; $d= sprintf("%s %02d %02d\n",$date[5]+1900,$date[4]+1,$date[3]); #print " \nView created on ----> $d \n"; @curt = $y; @crt = $b[0]; foreach(@curt) { @ncurt = (split/\s/,$_); foreach $nc (@ncurt) { # print "\n$nc"; } } foreach(@crt) { @ncrt = (split/\-/,$_); foreach $ncr (@ncrt) { #print "\n$ncr"; } } for($i=0;$i<=$#ncurt;$i++) { for($j=0;$j<=$#ncrt;$j++) { #print "\nValue of i is $i and j is $j"; $diffyr = $ncurt[0] - $ncrt[0]; #print "\nValue of i is $crt[1] and j is $curt[1]"; #print "\nYear difference is $diffyr \n"; $diffmon = $ncurt[1] - $ncrt[1]; if($diffmon < 0) { $diffmon =~ s/-//;} #print "\nMonth difference is $diffmon \n"; $diffdat = $ncurt[2] - $ncrt[2]; if($diffdat < 0) { $diffdat =~ s/-//;} #print "\nDate difference is $diffdat \n"; } } $diff = $diffyr." Yrs"." ".$diffmon." Months"." ".$diffdat." Days" +; # print "\nDifference between the dates: $diff \n"; print "\nAge: is $diff old \n"; print LOG "\nAge: is $diff old \n"; } close (LOG);

Replies are listed 'Best First'.
Re: date difference
by Anonymous Monk on Oct 06, 2009 at 15:19 UTC
    Why you don't want to use the module?

    What prevents you from using it? Tell us and we may have the solution

    What prevents you from using the code from the module, with Author's permission? In other words, why we should think for you and not spend our time in thinking something new and more important?

      Hi,

      Now I have modified the script as shown below. I am using for the first time, Please be kind to forgive mistakes.

      use Date::Calc qw(Delta_YMD); @date = localtime(); open(LOG, ">veiwreport.txt") or print die $!; @vlist = "raghavr_view11"; foreach(@vlist) { @viewdetails = `cleartool lsview -prop -full $_`; } foreach $vd (@viewdetails) { if ($vd =~ m/Last accessed/) { @details = split(/\s+/,$vd); $accdet = $details[2]; @acc = split(/T/,$accdet); chomp(@acc[0]); print "\nLast accessed : @acc[0]"; print LOG "\t Last accessed : @acc[0]"; $ac=@acc[0]; @ac = split(/\-/,$ac); $year1=@ac[0]; $month1=@ac[1]; $day1=@ac[2]; } if ($vd =~ m/Created/) { @a = split(".",$vd,1); @b = split("T",@a[0]); $b[0] =~ s/(\w)*/ /; $b[0] =~ s/\s+//; print " created on $b[0]"; print LOG " created on $b[0]"; $crd=@b[0]; @cd = split(/\-/,$crd); $year2=@cd[0]; $month2=@cd[1]; $day2=@cd[2]; } $diff = Delta_YMD($year1,$month1,$day1, $year2,$month2,$day2); print "Age of the $vd view is $diff \n"; }

      First of all, I am getting the error: Date::Calc::Delta_YMD(): not a valid date at rs.pl line 60. Please suggest, as I am really struck here.

      Hi, Sorry for the late response, I was not well. Yes, previously I had access problem on the server and installation of perl module on the server, but now I am permitted for the installation of perl modules. So now kindly suggest how to use the modules.

        Yes, even you can use CPAN

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: date difference
by mickep76 (Beadle) on Oct 09, 2009 at 11:34 UTC

    I usually make 2 functions 1 to convert to unix time the other to return a formated string. I usually prefer to avoid POSIX calls since that won't work on Windows. So I prefer to simply use Time::Local.

    To get the difference between 2 dates simply convert them both to seconds and then use some subtraction magic.

    use Time::Local; my %months = ( 'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7, 'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11 ); my $date = '2001-01-01'; my $nodays = int((get_unixtime($date) - time) / 60 / 60 / 24); printf "No days: $nodays\n"; # Convert date to number of seconds from 1970 sub get_unixtime { my $date = shift; # MM/DD/YYYY if($date =~ /(\d+)\/(\d+)\/(\d\d\d\d)/) { return timegm(0, 0, 0, $2, $1 - 1, $3); } # D MMM YYYY elsif($date =~ /(\d+) (\w\w\w) (\d\d\d\d)/) { return timegm(0, 0, 0, $1, $months{$2}, $3); } # YYYY-MM-DD elsif($date =~ /(\d\d\d\d)-(\d\d)-(\d\d)/) { return timegm(0, 0, 0, $3, $2 - 1, $1); } return 0; } # Convert number of seconds since 1970 to YYYY-MM-DD sub get_date { my $sec = shift; my @time = gmtime($sec); return sprintf "%04d-%02d-%02d", $time[5] + 1900, $time[4] + 1, $tim +e[3]; }