Find Day of Week


 
Thread Tools Search this Thread
Operating Systems HP-UX Find Day of Week
# 1  
Old 11-14-2012
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.

Code:
#!/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,

Code:
FNR==3

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

Breaking the math down:

Code:
(7-$NF)

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

Code:
((7-$NF)+DD)

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

Code:
((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! :-) ) I can't break it, but I've a nagging suspicion I'm missing something. Smilie

TIA
# 2  
Old 11-14-2012
Code:
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]}"

This User Gave Thanks to Yoda For This Post:
# 3  
Old 11-14-2012
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.
https://www.unix.com/302458026-post1.html
This User Gave Thanks to jgt For This Post:
# 4  
Old 11-15-2012
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) :-)
# 5  
Old 11-15-2012
Quote:
Originally Posted by rwuerth
Code:
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}')]}

I'm basically just wondering if anyone can break this given a modern date and valid inputs. (no using February 30th! :-) ) I can't break it, but I've a nagging suspicion I'm missing something. Smilie
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.
Code:
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:
Code:
cal $mth $yr | awk 'NR==2 {split($0, wk)} NR==3 {print wk[(7-NF+DD-1)%7+1]}' DD=$day

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 11-15-2012
Thanks Alister! I had briefly thought about using the cal output for day names, but my friend demanded FULL names! Smilie

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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the week's day

Hi All, I have the below requirement , if i give the week number for ex 41 i need to get the date for Monday and thursday for this given week. my expected output is 13/10/2014 (Monday's date) and 16/10/2014 (Thursday's date) I am using GNU LINUX . Pls help me with your thoughts. Thanks in... (7 Replies)
Discussion started by: mohanalakshmi
7 Replies

2. Shell Programming and Scripting

Reading in MM/DD/YY, find Day of Week

Hi everyone, I have a shell script that merges many files down in to one, then removes unwanted lines, that part is working fine: #!/bin/bash FILES=/home/pi/temp/qbd/* for f in $FILES do echo "Processing $f file..." # take action on each file. $f store current file name echo... (9 Replies)
Discussion started by: gjws
9 Replies

3. UNIX for Dummies Questions & Answers

Sudoers for one day per week?

I have been volunteered by my boss to be the sysadmin for our production redhat server. He asked me to tighten the security to avoid mishaps like "rm -f *" that occured not long ago. Right now, we have 53 users sudo-ing into the machine and it is an audit nightmare. I am wondering if it... (15 Replies)
Discussion started by: alan
15 Replies

4. Shell Programming and Scripting

Get day of week from cal

Hi all, I am trying to get dow from cal using below script #! /bin/bash YEAR=`echo $1 | cut -c 1-4` MONTH=`echo $1 | cut -c 5-6` DAY=`echo $1 | cut -c 7-8` for i in 1 2 3 4 5 6 7 do dayofweek=`cal $MONTH $YEAR | awk '$i == $DAY {printf("%s","$i")}'` echo $dayofweek... (4 Replies)
Discussion started by: bzylg
4 Replies

5. UNIX and Linux Applications

How to find 'Day of week' in Linux system

Hi All, I want to find a day of week for the Linux system. can some one help me on this.. Thanks in advance, Raji. (2 Replies)
Discussion started by: rajinavaneethan
2 Replies

6. HP-UX

Get Day of Week from date

Hi All, I have date in string format 'YYYY-MM-DD'. I want to know day of the week for this date. Example. For '2005-08-21' my script should return '0' or Sunday For '2005-08-22' it should return '1' or Monday I want piece of code for HP-UX korn shell. Appreciate reply on this. (5 Replies)
Discussion started by: vpapaiya
5 Replies

7. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies

8. Shell Programming and Scripting

Yesterday's Day of week

I need o get yesterday's day of week but im not exactly sure. the actual name is what i want. I can do it with numbers but im not sure with words. (3 Replies)
Discussion started by: rcunn87
3 Replies

9. Programming

Function that gets the day of the week (0-6) ??

Hi , I am working at Unix system,using c lang. I need c fun which return the day of the week . For example : 0- Sunday. 1- Monday. .... 10x. (4 Replies)
Discussion started by: kamil
4 Replies

10. UNIX for Dummies Questions & Answers

Calculating the day of the week

Hi all, I would like to calculate the day of the week using a supplied date. i.e. 20011012 = Day 5. Any ideas? Many thanks, ligs (4 Replies)
Discussion started by: ligs
4 Replies
Login or Register to Ask a Question