/* 
 * Copyright (C) Linux-Manipur 2005-2008 
 *
 * This program 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.
 *
 * The program finds the number of days between two given dates.
 * Be a good citizen and provide the valid date. 
 */  


#include <stdio.h>


typedef struct  
{
	int dd;
	int mm;
	int  yyyy;
} Date;

int isleapyear(int year );
long find_between_days( Date d1, Date d2);

int main ()
{
	Date d1, d2;
	printf ("Enter the first date in format ( dd mm yyyy ):");
	scanf ("%d", &d1.dd);
	scanf ("%d", &d1.mm);
	scanf ("%d", &d1.yyyy);

	printf ("Enter the second date in format ( dd mm yyyy ):");
	scanf ("%d", &d2.dd);
	scanf ("%d", &d2.mm);
	scanf ("%d", &d2.yyyy);

	printf ("The number of days between the two given dates is : %ld\n\n", find_between_days(d1,d2));

	return 1;
}

 long find_between_days( Date d1, Date d2)
{
	long days = 0;
	int months[14]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	int i;
	//Second date may be earlier than first. If so swap the dates.
	if ( d1.yyyy > d2.yyyy || (d1.yyyy == d2.yyyy && d1.mm > d2.mm ) || (d1.yyyy == d2.yyyy && d1.mm == d2.mm && d1.dd > d2.dd )) 
	{
		Date td = d1;
		d1 = d2;
		d2 = td;	
	}

	//Same month and year
	if (d1.yyyy == d2.yyyy && d1.mm == d2.mm) 
		return (d2.dd - d1.dd);

	if (isleapyear(d1.yyyy))
		months[1] = 29;

	days = months [ d1.mm -1] - d1.dd;

	d1.mm  = d1.mm +1;
	if (d1.mm > 12 ) {
		d1.yyyy = d1.yyyy + 1;
		d1.mm = 1;
	}

	while ( !(d1.yyyy ==d2.yyyy && d1.mm == d2.mm))
	{
		days += months [d1.mm -1] ;
		d1.mm  =  d1.mm+ 1;
		if (d1.mm > 12 ) {
			d1.yyyy = d1.yyyy + 1;
			d1.mm = 1;
		}

		if (isleapyear(d1.yyyy))
                	months[1] = 29;
		else
			months[1] = 28;

	}
	
	days += d2.dd;

	return days;	
	
}	

int isleapyear(int year )
{
	if ( year % 4 )
		return 0;
	
	if ( year % 400 == 100 || year %400 == 300)
		return 0;

	return 1;
}
	

