Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if today is the first weekday og the month

Status
Not open for further replies.

Larshg

Programmer
Joined
Mar 1, 2001
Messages
187
Location
DK
Hi

I need a peece of code that will tell me if today is the first weekday of the month.

I have to run a job the first weekday of the month. And for that I need something that tells me, if today is the first weekday or not.

Regards
Larshg
 
Assuming you have a C compiler on your UNIX box, you can compile this and call it from a shell script:
Code:
#include <time.h>

/*
 * Returns 1 if this is the first weekday of the month.
 */

int main( void )
{
  struct tm tms;
  time_t t = time(NULL);

  localtime_r( &t, &tms );

  switch ( tms.tm_mday )
  {
  case 1:

    if ( tms.tm_wday > 0 && tms.tm_wday < 6 )
      return 1;

  case 2:
  case 3:

    if ( tms.tm_wday == 1 )
      return 1;
  }

  return 0;
}

If you copy the code into a file named &quot;som.c&quot; (for Start Of Month), you can compile the code with:
Code:
cc -o som som.c
 
Larshg:

you can easily determine the first saturday of a month with the cal command. Given that:
$1=month
$2=day
$3=4-digit year
the last argument of this command is the date of the first
saturday of the month:
cal $1 $3|tail +3|head -1

#!/bin/ksh

dday=$2
set - $(cal $1 $3|tail +3|head -1)
echo $# # what day is the first saturday

# what's the first work day Mon .. Fri, fwday,
# when the first saturday is ..
case $# in # the first work
7) fwday=2;; # it's a monday
2|3|4|5|6) fwday=1;; # starts the 1st
1) fwday=3;; # the next mon
*) fwday=0;; #error
esac

if [[ $dday -eq $fwday ]]
then
echo &quot;This is the first work day&quot;
fi
# end script

I haven't tested this as thoroughly as I might have, but it looks good to me.

Regards,

Ed
 
Code:
#!/bin/sh
DAYNUM=`date +%e`
WEEKDAY=`date +%u`

if [ $DAYNUM -lt 8 -a $WEEKDAY -eq 1 ] ; then
echo &quot;This is the first weekday of this month&quot;
fi
 
#!/bin/sh

#---define function to see if today is the 1st working day of the month
function check_1st {
DoM=`date +'%d'`
DoW=`date +'%u'`
case $DoM in
01) case $DoW in
[1-5]) return 0 ;;
*) return 1 ;;
esac ;;
02|03) case $DoW in
1) return 0 ;;
*) return 1 ;;
esac ;;
*) return 1 ;;
esac
}

#---use it like this....
if check_1st
then
echo Today is the 1st working day of the month
else
echo Today is NOT the 1st working day of the month
fi
 
hmm ... you're right, just looking for the first monday ...

Code:
#!/bin/sh
DAYNUM=`date +%e`
WEEKDAY=`date +%u`

if [ $DAYNUM -eq 1 -a $WEEKDAY -le 5 -o $DAYNUM -le 3 -a $WEEKDAY -eq 1 ] ; then
echo &quot;This is the first weekday of this month&quot;
fi

hmm ...
 
Hi Larshg

Here we go!

#!/bin/ksh
WEEKDAY=`date +%w`
MONTHDAY=`date +%d`
if [[ $MONTHDAY -le 7 ]] then
if [[ $WEEKDAY = 0 ]] || [[ $WEEKDAY = 6 ]] then
echo &quot;this is not first weekday(working day)&quot;
else
echo &quot;this is first weekday(working day)...You can run the job or script!&quot;
/path_of_script_dir/file_name.ksh
fi
else
echo &quot;This is not the first week of month&quot;
exit 99
fi
#


sushveer
IBM certified specialist-p-series AIX5L System Administration
AIX/SOLARIS/WEBSPHERE-MQ/TIVOLI Administrator
 
actually you only need to check the first 3 days, like my example above ... because a weekend is only 2 days long.

so either the 1st day of the month is a weekday, or the second or the third are monday.

all 7 scenarios:

M T W <-- 1st day is weekday
T W T <-- 1st day is weekday
W T F <-- 1st day is weekday
T F S <-- 1st day is weekday
F S S <-- 1st day is weekday
S S M <-- 3rd day is monday
S M T <-- 2nd day is monday

 
Hi Jad

I am not considering that monday is the first working day of a month....First day of first week of month is suitable for backup as per Larshg.so I programmed in such a way that
first day may be any monday-friday..(excluding sat and sundays)

I hope this makes sense rather than prefering only monday for backup...Let Larsh decide...

sushveer
IBM certified specialist-p-series AIX5L System Administration
AIX/SOLARIS/WEBSPHERE-MQ/TIVOLI Administrator
 
1st day of 1st week ...

if the 1st is in a weekend then the next possible working day will be a monday.

if the 1st isn't a weekend then it is the first day, and then you don't have to worry because if you're only checking 3 days the other 2 CAN NOT be monday.

seems easiest to me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top