#!/usr/bin/perl
push(@INC,"/www/cgi-bin");
require "cgi-lib.pl";
&ReadParse;
print &PrintHeader;

# Initializes variables for local purposes

$td=$in{td};
$tm=$in{tm};
$ty=$in{ty};

# The list @DayNames lists the names of the days in order from Wednesday
# to Tuesday.  The reason behind this is because we used as our starting
# point Wednesday, January 1st, 1800.  In Mod 7 terms, a zero will denote
# no change in the day shift, hence a Wednesday.

@DayNames = ('Wednesday', 'Thursday', 'Friday', 'Saturday',
             'Sunday', 'Monday', 'Tuesday');

# The list @MonthNames is the same, except that is it used only for the
# HTML display.  The first element is Null, because Month[0] does not
# exist, and consequently Month[1] is January.

@MonthNames = ('Null', 'January', 'February', 'March', 'April', 'May',
               'June', 'July', 'August', 'September', 'October',
               'November', 'December');

# The list @MonthOffset is a bit tricky.  The first element is zero so
# that the first of the month will be 1, not 0.  The list denotes how
# many days the first of the corresponding month is shifted from the first
# of the year.  January 1st is not shifted (0), and since there are 31
# days in January, February 1st is shifted three days.  February has 28
# days, so it doesn't shift March at all (it remains three days).  This
# process continues up to December, which is shifted by five days.  The
# leap year is taken care of with $IsLeapYear.

@MonthOffset = (-1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5);

# $IsLeapYear checks to see if target year is divisible by four and
# adds one if true.  It subtracts one if target year is divisible by
# 100, and adds one if dibisible by 400.  The result is either 0 or 1.

$IsLeapYear = ($ty % 4 == 0) - ($ty % 100 == 0) + ($ty % 400 == 0);
$IsLeapYear *= ($tm >= 3);

# The following calculation will determine the number of the day.  The
# notes beside each calculation explain the process.

$diff = $ty - 1800;		# Difference between target and base years
				# Note, however, that all calculations are
				# performed on years before target year,
				# for those years directly affect the date

$day = ($diff			# Difference of years
     + int(($diff - 1) / 4)	# Plus number of leap years
     - int(($diff - 1) / 100)	# Minus number of years div. by 100
     + int(($diff + 199) / 400)	# Plus number of years div. by 400 (plus 199 to offset 1800)
     + $MonthOffset[$tm]	# Plus how much the month offsets day
     + $IsLeapYear		# Add one if current year is leap year
     + $td - 1			# Add difference of target and first day
     ) % 7;			# Mod 7 to find the day

# Prints out the HTML script to be read by the user's browser.  The
# current script isn't too fancy.  That'll be for another day.

print <<"EoF";
<html><head><title>The Day of the Week</title></head>

<body>
<center><h2>The Day of the Week</h2>
<p>$MonthNames[$tm] $td, $ty is a $DayNames[$day].</p>
<p><a
href="http://www.timeanddate.com/calendar/index.html?year=$ty&country=1">Click 
here</a> for a nice calendar of the year $ty.</p>
<p>This page was created by Brian Abram, Talia Riklis, and Jessica Weber.</p>
</center>
</body></html>
EoF
