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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AIX - date calculations 1

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
I need to write a script that will start on any day (Mon-Fri) and calculate the ending date for Friday.<br>
eg.<br>
# Get today day number<br>
DAYNO=4<br>
# Load start date = today<br>
STARTDATE=`date`<br>
# Get no of days until Friday<br>
DAYNO = 5 - $DAYNO<br>
# Load end date = friday<br>
ENDDATE=`date` + $DAYNO number of days<br>
<br>
I am using AIX 4.2 and Korn Shell.
 
i would probably have done something similar to:<br>
<br>
set `date`<br>
<br>
if ($1 == 'Mon')<br>
then<br>
$2 + 5 = date<br>
echo $date<br>
<br>
elif ($1 == 'Tues')<br>
then<br>
$2 + 4 = date<br>
echo $date<br>
<br>
... ETC<br>
<br>
<br>
<br>
<br>
-----------------------------------------------------------<br>
<br>
Mind you, you could use the switch statment??<br>
<br>
it is a while since i did scripting, but i can guess you can work out what i am doing this way.<br>
<br>
Just thought i'd try and help :))<br>
<br>
Karl<br>
<br>
Please send all death threats and such to the address at the bottome of the page! Thankyou! <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
P.S. You are remembering the #!/bin/ksh bit arnt you? <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
<br>
#!/usr/local/bin/perl<br>
#<br>
# print's Friday's date, no matter what day of the week today is.<br>
#<br>
<br>
$t = time;<br>
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t);<br>
<br>
$add_days = 5 - $wday;<br>
if ($add_days) {<br>
$add_secs = $add_days * 86400; # nbr secs in a day<br>
$t += $add_secs;<br>
}<br>
<br>
print scalar localtime($t);<br>

 
This could be more complex than first appears as you need to be aware of month boundaries, and year boundaries - eg. Monday is on 29th December and Friday is going to be in the following year. Added to that, in February you need to check for a leap year as well.

The following script should account for all this - it prints (on screen) the date of Friday irrespective of the day the script runs.

Hope its useful (if a little late!!).

Cheers,
Dave Vickers.

------------- START OF SCRIPT ------------------

#!/usr/bin/ksh

# File: findFriday
# Purpose: calculate date on Friday irrespective
# of start date.
#
# Not easy as you have to consider end-of-month
# boundaries and end-of-year boundaries.
#
# When checking end-of-month, check for leap year!

# Functions
friday () {
print &quot;Friday's date is: ${1}/${2}/20${3}&quot;
}

# Set Variables
MAX_DAYS=31
YEAR_TO_CHECK=`date +%y`
MONTH_TO_CHECK=`date +%m`
WEEKDAY_START=`date +%a`
CURRENT_DATE=`date +%d`
DAYS_TO_GO=0

case $MONTH_TO_CHECK in
02 ) {
if [ $(( $YEAR_TO_CHECK % 4 )) -eq 0 ]
then
MAX_DAYS=29
else
MAX_DAYS=28
fi
};;
04 ) MAX_DAYS=30;;
06 ) MAX_DAYS=30;;
09 ) MAX_DAYS=30;;
11 ) MAX_DAYS=30;;
esac

# We now have the maximum number of days in the month
# we are checking - hence if Friday is actually in the
# next month we can accomodate for it.
# Calculate number of days until Friday, based on
# current day of week.
case $WEEKDAY_START in
Sat ) DAYS_TO_GO=$(( $DAYS_TO_GO + 6 ));;
Sun ) DAYS_TO_GO=$(( $DAYS_TO_GO + 5 ));;
Mon ) DAYS_TO_GO=$(( $DAYS_TO_GO + 4 ));;
Tue ) DAYS_TO_GO=$(( $DAYS_TO_GO + 3 ));;
Wed ) DAYS_TO_GO=$(( $DAYS_TO_GO + 2 ));;
Thu ) DAYS_TO_GO=$(( $DAYS_TO_GO + 1 ));;
Fri ) DAYS_TO_GO=$(( $DAYS_TO_GO + 0 ));;
esac

# Variable $DAYS_TO_GO now holds the number of days
# until Friday.
# Now we get todays date and add DAYS_TO_GO to it,
# if it is greater than MAX_DAYS then Friday occurs
# next month.
# Also note that if we are in December then the next
# month is actually January and the year is incremented
# as well.
FRIDAY_DATE=$(( $CURRENT_DATE + $DAYS_TO_GO ))
if [ $FRIDAY_DATE -gt $MAX_DAYS ]
then
MONTH_TO_CHECK=$(( $MONTH_TO_CHECK + 1 ))
if [ $MONTH_TO_CHECK -gt 12 ]
then
MONTH_TO_CHECK=01
YEAR_TO_CHECK=$(( $YEAR_TO_CHECK + 1 ))
fi
FRIDAY_DATE=$(( $FRIDAY_DATE - $MAX_DAYS ))
fi
friday $FRIDAY_DATE $MONTH_TO_CHECK $YEAR_TO_CHECK

# END OF SCRIPT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top