
#include <stdio.h>
#include <stdlib.h>

/* #define DEBUG */

char Buffer[20];

char *DayNames[] = { "Date invalid!", "Monday", "Tuesday", "Wednesday",
					 "Thursday","Friday", "Saturday", "Sunday"};


char verstring[] = {"$VER: Date2Day v1.2 (08.12.96)"};


int d2d(const int day, const int month, const int year);
int datevalid(const int day, const int month, const int year);
int leapyear(const int year);

void main(int argc, char **argv)
{

	int day, month, year, d, i, j;

	if ((argc != 2) && (argc != 4))
		{
		printf("\nDate2Day v1.2 by Tadek Knapik.\n");
		printf("Usage: %s dd.mm.year\n\n", argv[0]);
		return;
		}

	if (argc == 4)
		{
		day = atoi(argv[1]);
		month = atoi(argv[2]);
		year = atoi(argv[3]);
		}
	else
		{
		i = 0;
		for (j = 0; (argv[1][i] != NULL) && (argv[1][i] != '.') && (argv[1][i] != '-'); ++i)
			Buffer[j++] = argv[1][i];
		if (argv[1][i] == NULL)
			return;

		Buffer[j] = NULL;
		++i;

		day = atoi(Buffer);	

		for (j = 0; (argv[1][i] != NULL) && (argv[1][i] != '.') && (argv[1][i] != '-'); ++i)
			Buffer[j++] = argv[1][i];
		if (argv[1][i] == NULL)
			return;

		Buffer[j] = NULL;
		++i;

		month = atoi(Buffer);

		for (j = 0; (argv[1][i] != NULL) && (argv[1][i] != '.') && (argv[1][i] != '-'); ++i)
			Buffer[j++] = argv[1][i];

		Buffer[j] = NULL;

		year = atoi(Buffer);
		}

	d = d2d(day, month, year);
	printf("%s\n", DayNames[d]);


}



/* Now, the d2d function itself */

int d2d(const int day, const int month, const int year)
{

	int centuries, years, i, j, k;

	int YearTables[7][7] = {
								{6, 0, 1, 2 ,3 ,4, 5},
								{5, 6, 0, 1, 2, 3, 4},
								{4, 5, 6, 0, 1, 2, 3},
								{3, 4, 5, 6, 0, 1, 2},
								{2, 3, 4, 5, 6, 0, 1},
								{1, 2, 3, 4, 5, 6, 0},
								{0, 1, 2, 3, 4, 5, 6}
							};

	int MonthTables[13][7] = {
								{0, 0, 0, 0, 0, 0, 0},
								{0, 1, 2, 3, 4, 5, 6},
								{3, 4, 5, 6, 0, 1, 2},
								{3, 4, 5, 6, 0, 1, 2},
								{6, 0, 1, 2, 3, 4, 5},
								{1, 2, 3, 4, 5, 6, 0},
								{4, 5, 6, 0, 1, 2, 3},
								{6, 0, 1, 2, 3, 4, 5},
								{2, 3, 4, 5, 6, 0, 1},
								{5, 6, 0, 1, 2, 3, 4},
								{0, 1, 2, 3, 4, 5, 6},
								{3, 4, 5, 6, 0, 1, 2},
								{5, 6, 0, 1, 2, 3, 4}
							};

	int LeapMonthTables[3][7] = {
								{0, 0, 0, 0, 0, 0, 0},
								{6, 0, 1, 2, 3, 4, 5},
								{2, 3, 4, 5, 6, 0, 1}
							};

	int DayTables[7][7] = {
								{6, 7, 1, 2, 3, 4, 5},
								{7, 1, 2, 3, 4, 5, 6},
								{1, 2, 3, 4, 5, 6, 7},
								{2, 3, 4, 5, 6, 7, 1},
								{3, 4, 5, 6, 7, 1, 2},
								{4, 5, 6, 7, 1, 2, 3},
								{5, 6, 7, 1, 2, 3, 4}
							};

	centuries = year/100;
	years = year%100;

#ifdef DEBUG

	printf("This is%s a leap year\n", leapyear(year)? "": " not");

#endif

	if(!datevalid(day, month, year))
		return 0;

/* date is ok, let's go */


	if ((year < 1582) || ((year == 1582) && ((month < 10) || ((month == 10) && (day < 15)))))
		i = centuries%7;
	else
		switch(centuries)
			{
				case 17: case 21: case 25:
					i = 0;
					break;
				case 18: case 22: case 26:
					i = 2;
					break;
				case 15: case 19: case 23: case 27:
					i = 4;
					break;
				case 16: case 20: case 24: case 28:
					i = 5;
					break;
			}

	j = (years + years/4)%7;
	k = YearTables[i][j];

	if ((leapyear(year) != 0) && (month <= 2))
		j = LeapMonthTables[month][k];
	else
		j = MonthTables[month][k];

	if	((i = day%7) != 0)
		--i;
	else
		i = 6;

	k = DayTables[i][j];
	return k;

}


/* check if given date is OK */

int datevalid(const int day, const int month, const int year)
{
	int i;

	if ((year > 2899) || (month == 0) || (month > 12) || (day == 0))
		return 0;

	if (month == 2)
	{
		if (leapyear(year)? (day > 29): (day > 28))
				return 0;
	}
	else
	{
		if ((i = month) > 7)
			--i;
		if (i%2? day > 31: day > 30)
			return 0;
	}
	return 1;
}

/* check if the year is a leap year */

int leapyear(const int year)
{
	return (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));	/* K&R */
}

