[comp.os.msdos.programmer] finding the day of week from the date

tr@samadams.princeton.edu (Tom Reingold) (06/22/91)

Here is a sample program that finds the day of the week, given the
date.  It works for this century only.  Making it work for any century
is "left as an exercise to the reader".

#include <stdio.h>
#include <sys/types.h>
#include <time.h>

static int monlens[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static char *daytab[] = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" };
static int keys[12];

void initkeys();

main(argc, argv)
int argc;
char *argv[];
{
    int day;
    time_t t;
    struct tm *tp;

    if (argc != 3 && argc != 4) {
yuck:
	fprintf(stderr, "usage: %s month date [year]\n", argv[0]);
	exit(1);
    }
    t = time(&t);
    tp = localtime(&t);
    tp->tm_mon = atoi(argv[1]) - 1;
    tp->tm_mday = atoi(argv[2]);
    if (argc == 4)
	tp->tm_year = atoi(argv[3]);
    initkeys(tp->tm_year);
    if (tp->tm_mon == -1 || tp->tm_mday == 0 || tp->tm_year == 0)
	goto yuck;
    if (tp->tm_mday > monlens[tp->tm_mon]) {
	fprintf(stderr, "%s: invalid date\n", argv[0]);
	exit(2);
    }
    day = dateof(tp);
    printf("%02d/%02d/%02d is a %s\n", tp->tm_mon + 1, tp->tm_mday,
	tp->tm_year, daytab[day]);
    exit(0);
}

dateof(tp) /* return day of week: Saturday=0, ..., Friday=6 */
struct tm *tp;
{
    int crap;

    crap = tp->tm_year + tp->tm_mday + keys[tp->tm_mon] +
     ((int) tp->tm_year / 4);
    return(crap % 7);
}

isleap(year)
int year;
{
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
	return(1);
    return(0);
}

void initkeys(n)
int n;
{
    int i;

    /* This generates a table.  I could hard code it, but I wanted
    to show what the table means. */
    if (isleap(n))
	keys[0] = 0, monlens[1] = 29;
    else
	keys[0] = 1;
    for (i = 1; i < 12; i++)
	keys[i] = (monlens[i-1] + keys[i-1]) % 7;
}
--
        Tom Reingold
        tr@samadams.princeton.edu  OR  ...!princeton!samadams!tr
        "Warning: Do not drive with Auto-Shade in place.  Remove
        from windshield before starting ignition."

gideon@cs.utexas.edu (Timothy Tin-Wai Chan) (06/22/91)

In article <tr.677526990@samadams> tr@samadams.princeton.edu (Tom Reingold) writes:
>Here is a sample program that finds the day of the week, given the
>date.  It works for this century only.  Making it work for any century
>is "left as an exercise to the reader".
                                  [...]

     Can you tell the day of the week by using DOS interrupts?  When you use
the DOS command DATE, it will say something like:

Current date is Sat  6-22-1991
Enter new date (mm-dd-yy):

     So apparently DOS finds the day of the week itself.  The question is,
how can I use this facility in DOS?


-- 
"I am immersed in sadness and joy                        | Timothy T. Chan
 and your touch brings me pain                           | gideon@cs.utexas.edu
 but your tears, Lord                                    | Dept. of CS
 wash all the shame away"        -- Sean Colletta (1991) | U of Texas at Austin

pshuang@athena.mit.edu (Ping-Shun Huang) (06/24/91)

In article <1468@ai.cs.utexas.edu> gideon@cs.utexas.edu (Timothy Tin-Wai Chan) writes:

 >      So apparently DOS finds the day of the week itself.  The question is,
 > how can I use this facility in DOS?

Have your program query DOS for the current date.  Save it.  Set the DOS
date to the date you want to know about.  Read the day-of-week (see
HELPPC or the commonly available interrupt list -- it's probably in the
BIOS data area if DOS does not provide an service which explicitly
returns it).  Set the DOS date back to the current date.  End.

Disadvantages: there are sharp limits to how early you can set the DOS
date (1-1-80 may well be the earliest date you can set it to, not
positive).  You're also taking a chance that Microsoft's code to
determine the day of week may be buggy.

--
Above text where applicable is (c) Copyleft 1991, all rights deserved by:
UNIX:/etc/ping instantiated (Ping Huang) [INTERNET: pshuang@athena.mit.edu]

mmccorm@d.cs.okstate.edu (McCormick Martin) (06/24/91)

I don't have the list of DOS function calls in front of me, right now, but
there definitely is a call which returns the current weekday in one of 
the registers.  0 = Sunday and 6 = Saturday.  I've written a couple of 
small applications using the function and it works very well.There are four
DOS functions grouped together.  They are called get date, set date, get
time, and set time.  I believe the one you want for the weekday is
get date.

Martin McCormick
Amateur Radio WB5AGZ
Oklahoma State University
Computer Center
Data Communication sGroup
Stillwater, OK