Author Message

To anyone reading this I need your help.  I am in the process of writing an
assembler program which based on input from the user outputs a calendar.
The user is prompted to input a month and a year.  The program then prints
out a calander of that particular month with the correct days of the week.

Now the problem is I can pund it out the long way by subtracting leap years,
etc...  But I have heard that there is a simple algabraic (sp.)  equation
which tells the day of any date in history.  If anyone knows this equation
or has any suggestions or helpful hints as to how to work this program I
would appreciate it greatly.

please E-mail it to me I need it fast.

-------------------------------------------------------------------------------
{*filter*} is the last refuge of the incompotent.
-the Foundation   Asimov
-------------------------------------------------------------------------------
peace out to all in 1991
from the "Walker"
CUL8R

Sat, 30 Apr 1994 00:58:06 GMT

Quote:
(WalkerBoh) writes:
>etc...  But I have heard that there is a simple algabraic (sp.)  equation
>which tells the day of any date in history.  If anyone knows this equation

/* This algorithm, `Zeller's congruence', is the usual - and fairly
* efficient - method of finding out on which day of the week a date
* will fall. Note that the year is not a 20th century year, but the
* FULL year number, e.g. `1989'.
*/

#include <stdio.h>

main(argc,argv)
int argc;
char **argv;
{
if (argc != 4)
{
fprintf(stderr,"Usage: zeller <day> <month> <year>\n");
}
else
{
char *day( );
int d, m, y;

d = atoi(*++argv);
m = atoi(*++argv);
y = atoi(*++argv);
printf("%d-%d-%d was a %s\n",d,m,y,day(d,m,y));
}

Quote:
}

char *day(d,m,y)
int d, m, y;
{
char *name_of_day( );
int day_0( );

return name_of_day(day_0(d,m,y),day);

Quote:
}

int day_0(d,m,y)
int d, m, y;
{
if (m < 3)
{
return day_1(d,m + 12,y - 1);
}
else
{
return day_1(d,m,y);
}

Quote:
}

int day_1(d,m,y)
int d, m, y;
{
/* Note: this is integer division, and the % symbol means
* modulus here.
*/

return (d + (13*m-27)/5 + y + y/4 - y/100 + y/400) % 7;

Quote:
}

char *name_of_day(d)
int d;
{
switch (d)
{
case 0:
return "sunday";

case 1:
return "monday";

case 2:
return "tuesday";

case 3:
return "wednesday";

case 4:
return "thursday";

case 5:
return "friday" ;

case 6:
return "saturday" ;
}

Quote:
}

--
Ken Johnson, A I Applications Institute //// `Always keep cool'
80 South Bridge, EDINBURGH EH1 1HN      ////   -- Wm Low's ham sandwich wrapper

031-650 2756 direct line                //// Muslims say: Hands Off Shoplifters

Sun, 01 May 1994 03:50:56 GMT

Quote:

>(WalkerBoh) writes:
>>etc...  But I have heard that there is a simple algabraic (sp.)  equation
>>which tells the day of any date in history.  If anyone knows this equation

>/* This algorithm, `Zeller's congruence', is the usual - and fairly
> * efficient - method of finding out on which day of the week a date
> * will fall. Note that the year is not a 20th century year, but the
> * FULL year number, e.g. `1989'.
> */
>[algorithm deleted]

I should point out that there is no easy way to deal with changes in
the calendars a few centuries ago.  Most julian/day-of-week algorithms
will not produce correct answers for dates before 1700 or so unless
they account for the calendar changes.  Unfortunately, these changes
took place at different times in different countries.  This should
not be a real problem for most software.

--
Scott Amspoker                       |
Basis International, Albuquerque, NM | This post stops at all

Mon, 02 May 1994 01:02:37 GMT

Now the problem is I can pund it out the long way by subtracting leap years,
etc...  But I have heard that there is a simple algabraic (sp.)  equation
which tells the day of any date in history.  If anyone knows this equation
or has any suggestions or helpful hints as to how to work this program I
would appreciate it greatly.

Here is a routine in PDP10 assembler (this -is- alt.lang.asm) to compute
the day of the week of January 1st of a given year:

; On entry, E contains year.
; Returns with day of week of January 1st of that year in B (Sunday = 0).
GDWOBY: MOVEI A,-1(E)
IDIVI A,400.
MOVEI A,1(B)
IDIVI B,4
IDIVI B,25.
SUB A,B
IDIVI A,7
POPJ P,

In C this would be:

int get_day_of_week_of_beginning_of_year(int year)
{
year = (year - 1) % 400;
return (year + 1 + (year / 4) - ((year / 4) / 25)) % 7;

Quote:
}

(Warning: I haven't checked the C code, but the assembler ran for years!)

- Alan

Mon, 02 May 1994 09:40:05 GMT

Quote:

>char *name_of_day(d)
>int d;
>{
>    switch (d)
>    {
>    case 0:
>            return "sunday";

>    case 1:
>            return "monday";

>    case 2:
>            return "tuesday";

>    case 3:
>            return "wednesday";

>    case 4:
>            return "thursday";

>    case 5:
>            return "friday" ;

>    case 6:
>            return "saturday" ;
>    }
>}

This would be better written as:

static char *name_of_day[7] = {"sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday"};

References to name_of_day(n) should be changed to name_of_day[n].
Using an array instead of a switch statement will make the resulting
program faster and smaller (unless you have a better compiler than
I've ever seen, and I've done a lot of research on switch statement
code generation).

Ellen Spertus

Wed, 04 May 1994 01:59:17 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages