How to find the first working day of month ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find the first working day of month ?
# 1  
Old 08-11-2010
How to find the first working day of month ?

Hi,

How to find the first working day of month ?

My requirement is, I need to call the function only if today is first working day of month. I could find out one function which finds last working day in month in this forum. Can anyone pls let me know for first working day. Thanks.

for last working day "
Code:
cal 8 2010 | awk '{ if(NF>1) a=$NF ; if (NF==7) a=$6}END{print a}'


Last edited by Franklin52; 08-11-2010 at 10:47 AM.. Reason: Please use code tags!
# 2  
Old 08-11-2010
Something like this?
Code:
cal 8 2010 | 
awk 'NR<3{next} {
  if(NF==1){
    next;print $1;exit
  }
  if(NF<7){
    print $1;exit
  }
  print $2;exit
}'

Or shorter:
Code:
cal 8 2010 | awk 'NR>2 && NF>1{print NF<7?$1:$2;exit}'


Last edited by Franklin52; 08-11-2010 at 01:31 PM..
This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 08-11-2010
bash:
Code:
days=(Sun Mon Tue Wed Thu Fri Sat)
months=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
for i in {1..12}; do
   read -a n2j <<<"$(tail -n +3 <(cal $i 2010))"
   case ${#n2j[@]} in
   1) pjo="Mon 3";; 
   7) pjo="Mon 2";;
   *) pjo="${days[7-${#n2j[@]}]} 1";;
   esac
   echo "${months[@]:i-1:1} $pjo"
done


Last edited by daPeach; 08-12-2010 at 06:08 PM..
# 4  
Old 08-11-2010
Hi all!
I think this is a very interesting problem Smilie It's closely related to some cron issues one might have.
So I thought about it "logically". If we by the first working day of a month we mean any Monday-Friday (days 1 to 5 in our calendar) that appears as the first day of the month, we have a hit. Not regarding any local holidays or calendars different than what we use here in Europe for example. Then the first work day can only appear as the 1st, 2nd or 3rd day of the month. If today is the first day of the month, and today is a work day, well then it is the first in the month. If today is the second or third day of the month, it can only be the first work day if it is a Monday.

So one way to establish if today is the first working day of the month could be something like this, it may be similar to other solutions presented. And a little bit verbose maybe, just to show the logic of it:

Code:
date +%e%t%u | while read dom dow; do
first=0
if [ $dom -eq 1 ] && [ $dow -le 5 ]; then
  first=1
elif [ $dom -le 3 ] && [ $dow -le 5 ]; then
  first=$dow
fi
if [ $first -eq 1 ]; then echo is first ;else echo is not first; fi
done

And then the value of $first can be used as true or false, 1=true, anything else is false. (The while construct is a lazy way to get around child vs. parent variable issues)

One way to test it is this little program that chews through dates 600 days into the future (just an example), to establish if it is the first work day in each month, and just compare it with a regular calendar. Etc, etc... :

Code:
for x in $(seq 1 600); do 
  date +%Y%t%B%t%d%t%u%t%A -d"now +$x day " | while read year month dom dow weekday; do 
    if [ $dom -eq 1 -a $dow -le 5 ] || [ $dom -le 3 -a $dow -eq 1 ] ; then 
      echo "In $month $year, a $weekday is the first working day of the month (monthday $dom, weekday $dow)"
    fi
  done
done

I use bash. I may have made some spleling errors, but it works on my machine Smilie

/L

---------- Post updated at 11:49 PM ---------- Previous update was at 09:41 PM ----------

Oi!

I just realised there was another funny use for this. Not sure if it's very effective to run the date command for every day, but anyway:

Code:
for x in {1..1000}; do 
  date +%Y%t%B%t%d%t%u%t%A -d"now +$x day " | while read year month dom dow weekday; do 
    if [ $dom -eq 13 -a $dow -eq 5 ] ; then 
      echo "$weekday $dom $month $year is a bad day"
    fi
  done
done

Output:
Code:
$ sh fredagendentrettonde.sh
Friday 13 August 2010 is a bad day
Friday 13 May 2011 is a bad day
Friday 13 January 2012 is a bad day
Friday 13 April 2012 is a bad day
Friday 13 July 2012 is a bad day

'nuff for now,
Off to bed Smilie

/Lakris

Last edited by Lakris; 08-11-2010 at 06:33 PM.. Reason: a twist on the second script... simplyfied if-statement
# 5  
Old 08-11-2010
If you are using ksh93 (Korn Shell 93) the solution is trivial
Code:
fwdom()
{
   # add error checking for $1 $2 (month, year)
   day=1
   dow=$(printf "%(%w)T" "${1}/1/${2}")
   (( dow == 0 )) && day=2
   (( dow > 5 )) && day=3
   printf "%d" ${day}
}

# example - print date of first working day of each month in 2010
for (( i = 1; i <= 12; i++))
do
   printf "%02d/%02d/%d\n" $i $(fwdom $i 2010) 2010
done

outputs
Code:
01/01/2010
02/01/2010
03/01/2010
04/01/2010
05/03/2010
06/01/2010
07/01/2010
08/02/2010
09/01/2010
10/01/2010
11/01/2010
12/01/2010

This User Gave Thanks to fpmurphy For This Post:
# 6  
Old 08-12-2010
Mind blowing replys and diff approaches..Thanks a ton!
# 7  
Old 08-12-2010
Hi again,
I just had to give another example, same as my first, but simplified and compacted, since I think the question was "Is today the first working day of the month?". Instead of echo true, one can use a return code or something.

Code:
dom=$(date +%d);dow=$(date +%u)
[ $dom -eq 1 -a $dow -le 5 ] || [ $dom -le 3 -a $dow -eq 1 ] && echo true

Smilie

/Lakris

Last edited by Lakris; 08-12-2010 at 06:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

2. Shell Programming and Scripting

Find Month first Working Day

Hi, I would like to calculate 1st working/Business day of each month. Exp: 1st -Oct-2011 is Saturday--- Non Business Day So the Next Working Day would be 3-Oct-2011 I need a shell script to calculate the month first business date. (3 Replies)
Discussion started by: koti_rama
3 Replies

3. UNIX for Dummies Questions & Answers

Running Script via Crontab on 2nd Working day each month

Hello Guys, I have a questions regarding running a shell script every second working day each month. I have no clue how solve this problem :wall:. Important is that it has to be the second working (Mo-Fr). Example: If 1st and 2nd Days of month are Sat and Sun the script must run on 4th day... (5 Replies)
Discussion started by: Hollo
5 Replies

4. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

5. Shell Programming and Scripting

how to find the second business day of month

I want to decide is that today is larger than the second business day of this month, who can i find second business day of this month? (3 Replies)
Discussion started by: qjlongs
3 Replies

6. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

7. Shell Programming and Scripting

last working day of previous month

Hi, I want a script(ksh) to see if today is the last working day(Mon-Fri) of the month. If it is the last working day I need to print current date, else I need the last working day of previous month. Thanks in advance. (1 Reply)
Discussion started by: rspk_praveen
1 Replies

8. Shell Programming and Scripting

Get Last working day of the month

Hi I need a script to get "Last working day of the month". I will pass the month and year as parameters and i need to get the last working date. Ex for June 2008 the last working day is 30th its monday. for August 2008 the last working day is 29th and it is Friday. ie the last working... (6 Replies)
Discussion started by: manmarirama
6 Replies

9. Shell Programming and Scripting

How to find the first day of previous month in unix?

How to find the first day of previous month in unix mmddyyyy format? example : today is 07052007 (in mmddyyyy) output sud be 06012007 thanks mohapatra (10 Replies)
Discussion started by: mohapatra
10 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question