Day of week routine

Randy_Davis rjd at occrsh.ATT.COM
Thu May 25 00:46:49 AEST 1989


In article <234 at zeek.UUCP> larry at zeek.UUCP (Larry Podmolik) writes:
|I need a C function that, given a date, would return what day of the
|week it fell on.  Of course, I didn't think to save it at the time.

  Well, this is not C, but it is a cheap and easy shell script that will
do the same, based on the "cal" command.

Randy Davis					UUCP: ...(att!)ocrjd!randy
						      ...(att!)occrsh!rjd
--------------------------cut here--------------------------------------
# How this for simple - "dow", a simple day of week calculator, written 1/11/89
# by Randy Davis.  Error checking and error messages added 3/9/89.

USAGE="Usage: $0 month[1-12] day[1-31] year[00-99] - to calculate the day\nof week for a certain date."

MONTH=$1
DAY=$2
YR=$3

case "$DAY" in
	[1-9]|[12][0-9]|3[01]) ;;
	*) echo $USAGE ; exit ;;
esac
case "$MONTH" in
	[1-9]|1[012]) ;;
	*) echo $USAGE ; exit ;;
esac
case "$YR" in
	[0-9][0-9]) ;;
	*) echo $USAGE ; exit ;;
esac

set `cal $MONTH 19$YR | egrep "^$DAY | $DAY | $DAY$"`

for DOW in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
  if [ "$1" = "$DAY" ]
  then
    break
  fi
  shift
done

echo $DOW



More information about the Comp.lang.c mailing list