/*  This file is part of lsnet. 

    Lsnet is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Lsnet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with lsnet.  If not, see <http://www.gnu.org/licenses/>.
*/

char datestring[1000];

//##### For date conversion ie: Jan -> 1
int months1(char month[4])
{
	int var;
	if (0 == strcmp( month, "Jan" )){
		var = 1;
	}else if ( 0 == strcmp( month, "Feb" ) ) {
			var = 2;
        }else if ( 0 == strcmp( month, "Mar" ) ) {
			var = 3;
        }else if ( 0 == strcmp( month, "Apr" ) ) {
                	var = 4;
        }else if ( 0 == strcmp( month, "May" ) ) {
                	var = 5;
        }else if ( 0 == strcmp( month, "Jun" ) ) {
                	var = 6;
        }else if ( 0 == strcmp( month, "Jul" ) ) {
                	var = 7;
        }else if ( 0 == strcmp( month, "Aug" ) ) {
                	var = 8;
        }else if ( 0 == strcmp( month, "Sep" ) ) {
                	var = 9;
        }else if ( 0 == strcmp( month, "Oct" ) ) {
                	var = 10;
        }else if ( 0 == strcmp( month, "Nov" ) ) {
                	var = 11;
        }else if ( 0 == strcmp( month, "Dec" ) ) {
                	var = 12;
	}else{   
		printf("Invalid Month String!\n");
		exit (1);
  	}
	return var;
}

//######## For days in month translation
int months2(int monthnumber)
{
	int days;
        if (monthnumber == 1){
                days = 31; 
        }else if ( monthnumber == 2 ) {
                days = 1;
        }else if ( monthnumber == 3 ) {
                days = 31;
        }else if ( monthnumber == 4 ) {
                days = 30;
        }else if ( monthnumber == 5 ) {
                days = 31;
        }else if ( monthnumber == 6 ) {
                days = 30;
        }else if ( monthnumber == 7 ) {
                days = 31;
        }else if ( monthnumber == 8 ) {
                days = 31;
        }else if ( monthnumber == 9 ) {
                days = 30;
        }else if ( monthnumber == 10 ) {
                days = 31;
        }else if ( monthnumber == 11 ) {
                days = 30;
        }else if ( monthnumber == 12 ) {
                days = 31;
        }else{
                printf("Invalid Month Number!\n");
                exit (1);
        }
	return days;
}
//####################

//##### For Hour format conversion 00:59:59 to 12am to 11pm
int hourhash(int hour1)
{
	//printf("HOUR1: %d\n", hour1);
	int hour2;
        if (hour1 == 00 || hour1 == 12){
                hour2 = 12;
        }else if (hour1 == 23 || hour1 == 11) {
               	hour2 = 11;
        }else if (hour1 == 22 || hour1 == 10) {
               	hour2 = 10;
        }else if (hour1 == 21 || hour1 == 9) {
               	hour2 = 9;
        }else if (hour1 == 20 || hour1 == 8) {
               	hour2 = 8;
        }else if (hour1 == 19 || hour1 == 7) {
               	hour2 = 7;
        }else if (hour1 == 18 || hour1 == 6) {
               	hour2 = 6;
        }else if (hour1 == 17 || hour1 == 5) {
               	hour2 = 5;
        }else if (hour1 == 16 || hour1 == 4) {
               	hour2 = 4;
        }else if (hour1 == 15 || hour1 == 3) {
               	hour2 = 3;
        }else if (hour1 == 14 || hour1 == 2) {
               	hour2 = 2;
        }else if (hour1 == 13 || hour1 == 1) {
               	hour2 = 1;
        }else{
		//printf("HOUR2: %d\n", hour2);
                printf("Invalid Hour Number!\n");
                exit (1);
        }
	return hour2;
}

//#########Sub to say if it's AM or PM
int ampm(int hourstring) 
{
	//printf("hourstring: %d\n", hourstring);
	int isam;
	if (hourstring < 12)
	{
		isam = 1;
	}else if (hourstring >= 12){
		isam = 0;
	}else{
		printf("Invalid Hour Number!\n");
		exit (1);
	}
	return isam;
}
//##########




//##########Returns the number of days in any month###############
int months3(int month, int year) {
        int daysinmonth;
	int isleap = 100;	

        int modulo1 = year % 4;
        int modulo2 = year % 100;
        int modulo3 = year % 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;
	}

	//printf("isleap: %d\n", isleap);
   	//1. Every year that is divisible by four is a leap year;
   	//2. of those years, if it can be divided by 100, it is NOT a leap year, unless
   	//3. the year is divisible by 400. Then it is a leap year.

	if (isleap == 1 && month == 2)
	{
		daysinmonth = 29;
	}else if (isleap == 0 && month == 2) {
        	daysinmonth = 28; 
	}else{
		daysinmonth = months2(month);
	}
	return daysinmonth;
}

//############



//##########The main routine

int dates (char datestring[1000]) 
{
	int minsince = 0;
        char *token;
        char *line;
        char *search = " ";
	char monthcur[100];
	char temp[10];
	char totaltimestring[8];
	int daycur;
	char timecurtmp[3];
	int hourcur;
	int mincur;
	int seccur;
	int yearcur;
	int yearsince;
	int monthsince;
	int year;
	int month;
	int day;
	int min;
	int daysinmonth;
	//int emptyvar;
	int count2 = 0;
	//int minsince;


	//Tue Feb 24 10:31:44 2009 2075412 217844 710552 328868 1857568 10980276 10605956 374320	 

	//printf ("DATESTRING: %s\n", datestring);

	token = strtok(datestring, search);
	//token = strtok(NULL, search);

	//fprintf(monthcur, "%s\n", token );
	//strcpy(monthcur, token);
	token = strtok(NULL, search);

	temp[0]=token[0];
	temp[1]=0;
	strcpy(monthcur,temp);
	for (count2 = 1; count2 <= strlen(token) && token[count2] != '\0' ; count2++)
	{
		temp[0]=token[count2];
		temp[1]=0;
		strcat(monthcur,temp);
	}

	//Tue Feb 24 10:31:44 2009 2075412 217844 710552 328868 1857568 10980276 10605956 374320	
	//printf ("TOKEN: %s\n", token);
	//printf ("monthcur: %s\n", monthcur);
	//fprintf(daycur, "%s\n", token );
	token = strtok(NULL, search);

	//temp[0]=token[count2];
	//temp[1]=0;
	//strcat(monthcur,&temp);
	daycur = atoi(token);
	//printf ("daycur: %d\n", daycur);

	//fprintf(totaltimestring, "%s\n", token );	
	token = strtok(NULL, search); //10:31:44

        temp[0]=token[0];
        temp[1]=token[1];
	temp[2]=0;
	//temp[3]=0;
	//strcat(hourcur,&temp);
	hourcur = atoi(temp);
	//printf ("hourcur: %d\n", hourcur);

        temp[0]=token[3];
        temp[1]=token[4];
        temp[2]=0;
        //temp[3]=0;
        //strcat(hourcur,&temp);
	mincur = atoi(temp);
	//printf ("mincur: %d\n", mincur);

        temp[0]=token[6];
        temp[1]=token[7];
        temp[2]=0;
        //temp[3]=0;
        //strcat(mincur,&temp);
	seccur = atoi(temp);
	//printf ("seccur: %d\n", seccur);
	//printf ("TOKEN: %s\n", token);
	token = strtok(NULL, search);

	yearcur = atoi(token);
	//printf ("yearcur: %d\n", yearcur);	
	//fprintf(yearcur, "%s\n", token );

	//strcat(timecurtmp, atoi(totaltimestring[0]));
	//strcat(timecurtmp, atoi(totaltimestring[1]));
	//hourcur = atoi(timecurtmp);

        //strcat(timecurtmp, atoi(totaltimestring[2]));
        //strcat(timecurtmp, atoi(totaltimestring[3]));
        //mincur = atoi(timecurtmp);

        //strcat(timecurtmp, atoi(totaltimestring[4]));
        //strcat(timecurtmp, atoi(totaltimestring[5]));
        //seccur = atoi(timecurtmp);

	month = months1(monthcur);
	//printf ("month: %d\n", month);

	//###calculate the number of full years since jan 1st 2009	

	yearsince = yearcur - 2009;
	//##0 for 2009
	//##1 for 2010
	//##2 for 2011
	//#365 days in 2009 because it's not a leap year
		
	monthsince = yearsince * 12;
	
	year = 2009;
	

	//#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++)
		{		
			daysinmonth = months3(month, year);
			minsince = minsince + (daysinmonth * 1440);
		}
	}

	//printf ("minsince: %d\n", minsince);
	//# 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


	//printf ("monthcur: %d\n", monthcur);
	//printf ("monthsince: %d\n", monthsince);
	monthsince = monthsince + month;
	//printf ("monthsince: %d\n", monthsince); //this is right
	//printf ("minsince1: %d\n", minsince);
	for(month = 1; month < monthsince; month++)
        {
	       	daysinmonth = months3(month, year);
		//printf ("daysinmonth: %d month: %d year: %d\n", daysinmonth, month, year);
               	minsince = minsince + (daysinmonth * 1440);
       	}

	//printf ("minsince2: %d\n", minsince);  //should be 79836 but is 84960

	//# 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

	day = daycur;
	day = day * 1440; 
        min = mincur + ((hourcur * 60) + (seccur / 60)); //#convert hours/sec to min
	minsince = minsince + (min + day);
		
	//#at this point $minsince = the total number of minutes since jan 1st 2009
	//#print "MIN SINCE JAN 1st 2009: $minsince\n";


	//minsince = 100;

	return minsince;
}

