Comparing the dates with the current date in perl scripting

Hi

i have a file containg dates likebelow

4/30/2013
3/31/2013
4/30/2013
4/16/2013
4/30/2013
4/30/2013
5/30/2013
5/30/2013
4/30/2013
5/30/2013
5/30/2013
3/31/2013

now i want to compare the above dates with current date and i want to display the difference .

eg: take first date in the file i.e 4/30/2013 and current date is 4/18/2013 i want output like 12 days left from today ...... like this iwant all the dates in the file to be compared withe current date and display the difference...

please friends help me

$ cat dates.sh
today_seconds=`date "+%s"`
seconds_in_day=86400
while read date; do
  month=`echo $date | cut -d "/" -f 1`
    day=`echo $date | cut -d "/" -f 2`
   year=`echo $date | cut -d "/" -f 3`

  seconds=`date -d "$year-$month-$day" "+%s"`
  diff=`expr $today_seconds - $seconds`
  days=`expr $diff / $seconds_in_day`
  echo days between today and $date = $days
done < dates
$ ./dates.sh
days between today and 4/30/2013 = -11
days between today and 3/31/2013 = 18
days between today and 4/30/2013 = -11
days between today and 4/16/2013 = 2
days between today and 4/30/2013 = -11
days between today and 4/30/2013 = -11
days between today and 5/30/2013 = -41
days between today and 5/30/2013 = -41
days between today and 4/30/2013 = -11
days between today and 5/30/2013 = -41
days between today and 5/30/2013 = -41
days between today and 3/31/2013 = 18
$ uname
Linux

hi thank you very much

But i need this to in perl script....
could you please write the script in perl.....please

No, I can't write the script in perl. Sorry about that. For someone who knows perl, it should not be a big deal to translate the logic into perl.

1 Like

Hi friends, is there any one to help me in perl scripting for the above?

I apologize for not seeing that the post subject clearly said "in perl scripting". :mad:

Isn't there anyone on the forum who does perl scripting who could take a look? The program logic is all there.

I think you had some impossible dates in there.

#!/usr/bin/perl

use Time::Local;
use POSIX qw(strftime);

while($line=<STDIN>)
{
        my ($mon, $mday, $year)=split(/\//, $line);
        my $timestamp=timelocal(0,0,0,$mday,$mon,$year);
        my $diff=($timestamp - time());
        chomp($line);
        print $line, " is ",int($diff/(60*60*24)), " days away\n";
}
$ ./tme.pl  <<EOF
4/30/2013
4/30/2013
4/16/2013
4/30/2013
4/30/2013
5/30/2013
5/30/2013
4/30/2013
5/30/2013
5/30/2013
EOF
4/30/2013 is 41 days away
4/30/2013 is 41 days away
4/16/2013 is 27 days away
4/30/2013 is 41 days away
4/30/2013 is 41 days away
5/30/2013 is 72 days away
5/30/2013 is 72 days away
4/30/2013 is 41 days away
5/30/2013 is 72 days away
5/30/2013 is 72 days away

$
4/30/2013 is 41 days away

I thought 4/30/2013 would be 12 days away. Maybe I'm missing something...

Thank you for taking on the script. I felt kind of bad starting to help, and not finishing.

hi thank you friend,
but if you see the output 4/30/2013 is just 11 days away from todays date, but the above script is showing something like 41 days away...

i request you please modify the script please...

[edit] that was optimistic, bug remains, one moment... But it was using a weird calendar since it got dates it considered 1900 years in the future...

Assuming that your dates are valid:

perl -MTime::Local=timelocal_nocheck -F/ -lape '$_ .= " --> " . (
timelocal_nocheck(0,0,0,$F[1],$F[0]-1,$F[2]-1900) - timelocal_nocheck(0,0,0,(localtime)[3,4,5])
)/(60*60*24)' file

producing (for 4/19/2013)

4/30/2013 --> 11
3/31/2013 --> -19
4/30/2013 --> 11
4/16/2013 --> -3
4/30/2013 --> 11
4/30/2013 --> 11
5/30/2013 --> 41
5/30/2013 --> 41
4/30/2013 --> 11
5/30/2013 --> 41
5/30/2013 --> 41
3/31/2013 --> -19
1 Like