Computing.Net > Forums > Unix > Date in Unix using Korn Shell

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

Date in Unix using Korn Shell

Reply to Message Icon

Original Message
Name: Pavel
Date: May 9, 2002 at 15:14:11 Pacific
Subject: Date in Unix using Korn Shell
Comment:

I am breaking my brains on creating a program, using the KSH, that will display the current date and the subsequent 7th date for the next (5) weeks.

My objective is to figure out the date for any five dates that are one week apart
beginning at any arbitrary date, keeping in mind different number of days in months of a year and leap years.

Eg: Today is Thursday May 9th, 2002. I need the program to show dates for five consecutive Thursdays in the future. The program should work on any given date that the system generates.

Can anybody help, please
Thank you.


Report Offensive Message For Removal


Response Number 1
Name: PaulS
Date: May 10, 2002 at 09:00:30 Pacific
Reply: (edit)

I think the logic might go like this:

1) get the current date, month and year and assign each to variables
2) test year for leap year. I forget--is it every 4 years, then modulo division might do it.
((X = YYYY % 4))
if X is 0 then leap year.
use case statement to match current month and set leap year maxdays in month.

else use second case statement to set normal year maxdays in month

3)now it a matter of addition--current date + 7 until you spill over into the next month (you know the maxdays in the current month). You may have to repeat the case statements again, but it would be better to include a small function which would return maxdays for the given month.


Report Offensive Follow Up For Removal

Response Number 2
Name: Jason
Date: May 10, 2002 at 09:53:40 Pacific
Reply: (edit)

Im going on lunch in a minute, but Ill be back after 2p (EST) Ill try to get a script working, doesnt seem like it will be that hard.



Report Offensive Follow Up For Removal

Response Number 3
Name: Rex
Date: May 13, 2002 at 19:02:24 Pacific
Reply: (edit)

#This one gives you a day on any given date #from 0001 AD and the dates of the next 5 #weeks.
#Usage is:
#Programname DayOftheMonth Monthoftheyear #Year
#eg dates 26 04 2004


# This function returns the number of days per month
# Requires month and year. NOTE No leading zeros
# Usage dayspm 5 2002

dayspm () {
case $1 in
4|6|9|11) ndays=30
;;
1|3|5|7|8|10|12) ndays=31
;;
2)
if [ `echo "scale=0; $2%4" | bc` -eq 0 ]; then
ndays=29
else
ndays=28
fi
;;
*)
echo "Month Input error"
exit 1
;;
esac
return $ndays
}

if [ $# -eq 3 ]; then
cal $2 $3
day=0
week=`cal $2 $3 | grep -w $1`
year=$3
for pos in 1 4 7 10 13 16 19
do
day=`expr $day + 1`
pos1=`expr $pos + 1`
dpos=`echo "$week" | cut -c$pos-$pos1`
dpos=`echo $dpos`
if [ "$1" = "$dpos" ]; then
case $day in
1) echo "That date is a Sunday"
;;
2) echo "That date is a Monday"
;;
3) echo "That date is a Tuesday"
;;
4) echo "That date is a Wednesday"
;;
5) echo "That date is a Thursday"
;;
6) echo "That date is a Friday"
;;
7) echo "That date is a Saturday"
;;
*) echo "Woooops day error"
;;
esac
month=`expr $2 + 0`
pmonth=$month
ndays=`dayspm $month $3;echo $?`
echo "Next 5 weeks are: "
for nxwk in 7 14 21 28 35
do
week=`expr $1 + $nxwk`
if [ $week -gt $ndays ]; then
month1=`expr $month + 1`
if [ $month1 -gt 12 ]; then
month1=1
fi
pmonth=$month1
week=`expr $week - $ndays`
daysnm=`dayspm $month1 $3;echo $?`
if [ $week -gt $daysnm ]; then
week=`expr $week - $daysnm`
pmonth=`expr $pmonth + 1`
fi

fi
echo "${week}/${pmonth}"
done
fi
done
else
echo Usage $0 day month year
exit 1
fi


Report Offensive Follow Up For Removal

Response Number 4
Name: sn
Date: July 25, 2002 at 02:39:46 Pacific
Reply: (edit)

How do you write a script to find a file today with yesterdays date. eg. i am trying to rcp a file with yesterdays date on the file, without having the users to input the date. thanks


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software