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.
in awk,
means I skip the headers and am only concerned with the first week of the 'cal' output.
Breaking the math down:
Is an offset to "normalize" partial weeks since the first week of a month may have less than 7 days in it.
The offset is added to the numerical day of the month (DD)
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.
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
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) :-)
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.
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.
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:
Regards,
Alister
Thanks Alister! I had briefly thought about using the cal output for day names, but my friend demanded FULL names!
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.
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)
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)
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)
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)
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)
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)
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)
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)