##### For date conversion ie: Jan -> 1
my %months1 = (
        Jan => '1',
        Feb => '2',
        Mar => '3',
        Apr => '4',
        May => '5',
        Jun => '6',
        Jul => '7',
        Aug => '8',
        Sep => '9',
        Oct => '10',
        Nov => '11',
        Dec => '12',
    );
##### For days in month translation
my %months2 = (
        1 => '31',
        2 => '1',   
        3 => '31',   
        4 => '30',
        5 => '31',
        6 => '30',
        7 => '31',
        8 => '31',
        9 => '30',
        10 => '31',
        11 => '30',
        12 => '31',
   );
########

##### For Hour format conversion 00:59:59 to 12am to 11pm
my %hourhash = (
        00 => '12',
        23 => '11',
        22 => '10',
        21 => '9',
        20 => '8',
        19 => '7',
        18 => '6',
        17 => '5',
        16 => '4',
        15 => '3',
        14 => '2',
        13 => '1',
        12 => '12',
	11 => '11',
        10 => '10',
        9 => '9',
        8 => '8',
        7 => '7',
        6 => '6',
        5 => '5',
        4 => '4',
        3 => '3',
        2 => '2',
        1 => '1',
   );
#########Sub to say if it's AM or PM
sub ampm {
	my($isam);
	my $a = sprintf("%d",$_[0]);
	if ($a < 12)
	{
		$isam = 1;
	}else{
		$isam = 0;
	}
	return($isam);
}
##########

##########Returns the number of days in any month###############
sub months3 {
        my($daysinmonth);
	my $a = sprintf("%d",$_[0]);
	my $b = sprintf("%d",$_[1]);
	my $isleap = 100;	

        my $modulo1 = $b % 4;
        my $modulo2 = $b % 100;
        my $modulo3 = $b % 400;

	#leap years  2000 2004 2008
	#not leaps 2009 2002 1999
        if ($modulo1 == 0) 
        {
		$isleap = 1;
		if ($modulo2 == 0)
        	{
                	$isleap = 0;
	                if ($modulo3 == 0)
	                {
        	                $isleap = 1;
                	}

        	}
	}else{
		$isleap = 0;
	}

	if ($isleap == 1)
	{

		if (exists $months2{2})
		{  
			$months2{2}=29;
		}

	}else{
		if (exists $months2{2})
		{
        		$months2{2}=28;
		}    
	}


	$daysinmonth = $months2{$a};
	return($daysinmonth);
}

############



##########The main routine
sub dates {
	my ($minsince);
	#open file
	#open "dates", "<dates.txt";
	#going to be given $var
        #my $a = sprintf("%d",$_[0]);
        #my $b = sprintf("%d",$_[1]);
	#chomp($_);
	#my ($line) = @_;
	#chomp($_)
	my $line = sprintf("%d",$_[0]);
	 
		#Thu Feb  5 08:46:41 2009 
		$line =~ /.{3}\s+(.{3})\s+(\d{1,2})\s+(\d{2}).(\d{2}).(\d{2})\s+(\d{4})\s/;
		my $monthcur = sprintf("%s",$1);
		my $daycur = sprintf("%d",$2);
		my $hourcur = sprintf("%d",$3);
		my $mincur = sprintf("%d",$4);
		my $seccur = sprintf("%d",$5); #seconds are ignored to account for program runtime variances
		my $yearcur = sprintf("%d",$6);
		$monthcur = $months1{$monthcur};

		#print "$monthcur $daycur $hourcur:$mincur:$seccur $yearcur\n";


		###calculate the number of full years since jan 1st 2009	
		my $yearsince = $yearcur - 2009;
		##0 for 2009
		##1 for 2010
		##2 for 2011
		#365 days in 2009 because it's not a leap year
		
		my $monthsince = $yearsince * 12;
		#$month = 1;
		my $year = 2009;
		#my $minsince = 0;

		#if year is 2009 it will fail, if 2010 it'll grab total min for all of 2009

	        for($year = 2009; $year < $yearcur; $year++)
	        {
			for($month = 1; $month <= 12; $month++)
			{		
				my($daysinmonth) = &months3($month, $year);
				$minsince += $daysinmonth * 1440;
			}
		}

		# at this point $minsince = the total number of minutes in the full years since jan 1st 2009

		####calculate the number of full months since jan 1st of current year	

		$year = $yearcur;
		#if it is january it will fail, otherwise counts all upto monthcur
		for($month = 1; $month < $monthcur; $month++)
	        {
		       	my($daysinmonth) = &months3($month, $year);
                	$minsince += $daysinmonth * 1440;
        	}
		# at this point $minsince = the total number of minutes in the full years since jan 1st 2009, plus any full months so far this year
		#print "minsince should be 44,640: $minsince\n";

		####calculate the number of min since the beginning of current month

		my $day = $daycur;
		$day = $day * 1440; 
	        my $min = $mincur + (($hourcur * 60) + ($seccur / 60)); #convert hours/sec to min
		$minsince += ($min + $day);
		
		#at this point $minsince = the total number of minutes since jan 1st 2009

		#print "MIN SINCE JAN 1st 2009: $minsince\n";


	#close file
	#close "dates";
	#remove file        
	#unlink "dates.txt";
	return($minsince);

}

