Find Day of Week

In HP-UX the date command does not have the "-d" switch like some other *nixes do. I'm working a simple script to tell me, given the day, month and year what day of the week that falls on.

Assuming valid day, month and year input (I'd perform quality checks on the input separately, but not shown here), would the following work for all dates? Or are there some corner cases that may not work correctly? I'm not worried about corner cases like way back in the 1500's when the Gregorian calendar 1st came online and how that might be screwed up.

The output is the output from a call to the 'cal' utility, plus the full name of the day of the week so that the named day of the week can be easily verified.

#!/bin/sh
day=$1
mth=$2
yr=$3
 
cal $mth $yr
 
set -A _WKDY Saturday Sunday Monday Tuesday Wednesday Thursday Friday
 
print ${_WKDY[$(cal $mth $yr | awk -vDD=$day 'FNR==3 {print ((7-$NF)+DD)%7}')]}

in awk,

FNR==3

means I skip the headers and am only concerned with the first week of the 'cal' output.

Breaking the math down:

(7-$NF)

Is an offset to "normalize" partial weeks since the first week of a month may have less than 7 days in it.

((7-$NF)+DD)

The offset is added to the numerical day of the month (DD)

((7-$NF)+DD)%7

The offset plus DD is then modded by 7 giving the numerical day of the week, of which, 0 is defined as Saturday, and 1-6 is defined as Sunday through Friday by how I set up the array.

I'm basically just wondering if anyone can break this given a modern date and valid inputs. (no using February 30th! :slight_smile: ) I can't break it, but I've a nagging suspicion I'm missing something. :confused:

TIA

dd=$1
mm=$2
yy=$3

index=$( echo "$dd $mm $yy" | awk ' {
dd=$1;mm=$2;yy=$3;
if(mm<3) { mm+=12; yy--; }
print int(yy/400 - yy/100 + (mm+1)*26/10 + yy*125/100 + dd - 1) % 7
} ' )

index=`expr $index + 1`
if [ $index -eq 7 ]; then
index=0
fi

declare -a day_arr
day_arr=( "Saturday" "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" )

echo "Day: ${day_arr[$index]}"
1 Like

You might find this useful. I use it to validate input dates, first convert the date to the number of days since Jan 1 1900, then convert that number back to a date, and compare with input.

1 Like

Thanks for the replies. I should state, that this is just a fun exercise thing, it's not for production use.

The gist of the problem is can one tell the day of the week if given a valid date in a "one liner", using the posix shell, cal and awk in HP-UX?
perl is not allowed because it can be done easily in perl.

"One Liner" is defined to allow for previously set variables.

So my "script" above is just a test bed for my one liner.

I feel pretty good about it, and was hoping to see if anyone could break it with valid date input, before I submit it to my friend and tell him where he's taking me to lunch! (oh yeah ... lunch is riding on this one) :slight_smile:

I don't see anything wrong with your code (given the stipulations in place).

If you like, you can just print the day of the week directly from AWK.

cal $mth $yr | awk 'NR==3 {split("Sat Sun Mon Tue Wed Thu Fri", wk); print wk[(7-NF+DD)%7+1]}' DD=$day

The additional +1 shift is needed because split()'s result begins at offset 1 in the array.

Some cals print unambiguous column headers, such as "Su Mo Tu We Th Fr Sa". When that's the case, and if they are sufficient, then one can use them directly instead of splitting a hardcoded list:

cal $mth $yr | awk 'NR==2 {split($0, wk)} NR==3 {print wk[(7-NF+DD-1)%7+1]}' DD=$day

Regards,
Alister

1 Like

Thanks Alister! I had briefly thought about using the cal output for day names, but my friend demanded FULL names! :rolleyes:

I did not think about having awk handle a names list, with the split function. I do like that, however since I have to use full names, that would really increase the length of the line, not that there is any imposed limit other than the system line size limit. But a shorter line will help prevent me from losing on a typo. :smiley: