Friendz.. plz help me on this date function.

:confused: Hi there.
Hi frndz,
I have to script a shell in such a way that by giving the current date, it should give the previous saturday date and next sunday date as output.
eg: Input - 01-01-2008
O/p -
last saturady- 30-12-2007(ddmmyy)
Next Sunday- 05-01-2008

Please help me in finding hw to do do it..plz.........

check if you can find a solution here

I'm also looking for a shell script that will return the previous Saturday's date from any given date.

Tried to look everywhere but cannot find any solution. I don't want to use the existing datecalc function.

Thank you.

What platform are you on...because GNU's date utility can do this for you quite easily.

aix...

please provide example of GNU's date utility regardless and this might give me a clue.

Thank you.

Please understand that I do not mean to distract from your desire for a script, but this is an excellent example, I think, of something which is bothersome with scripting but simple and straightforward with a C compiler. time(), localtime(), and the other date subroutines all give us excellent means to do this. perl or php also let you do fun things with dates. the UNIX "date" function kinda works with "now", unless of course you use the [-d|--date] option, but I don't see a simple way to make that work either. I had thought that if "date -d" would accept a julian date, then one could subtract a value related to the +%w value, but my CentOS 5 date program wouldn't take a julian date as input.

Sorry, I guess I just spoiled myself by writing C to do things like this. IMHO you might want to consider writing something in C, then using it as your date generator. If you are not comfortable with that, then perhaps perl or php might be helpful. Both are very powerful tools.

Hi, pchang.

Here are a few phrases date can understand and one that it cannot:

#!/bin/bash -

# @(#) s1       Demonstrate GNU date flexibility.

head -1 /etc/issue.net

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) date
set -o nounset
echo

echo " GNU date with English-like expressions:"
echo

date --date="2 weeks ago"
date --date="two weeks ago"

echo
date --date="today + 1 month"
date --date="3 years + 2 days"

exit 0

Producing:

$ ./s1
CentOS release 5 (Final)

(Versions displayed with local utility "version")
Linux 2.6.18-53.1.13.el5vm
GNU bash 3.1.17
date (GNU coreutils) 5.97

 GNU date with English-like expressions:

Sat Jul  5 15:55:46 CDT 2008
date: invalid date `two weeks ago'

Tue Aug 19 15:55:46 CDT 2008
Thu Jul 21 15:55:46 CDT 2011

As you can see, GNU date is extremely flexible.

I don't have access to an AIX box, but I suggest you look over man date to see if it has anything close ... cheers, drl

AIX has ksh93, so the following should work

$ printf "%T\n" "01012008"
Tue Jan  1 20:08:00 EST 2008
$ printf "%T\n" "01012008 last saturday"
Sat Dec 29 20:08:00 EST 2007
$ printf "%T\n" "01012008 next sunday"
Sun Jan 13 20:08:00 EST 2008
$ printf "%T\n" "01012008 this sunday"
Sun Jan  6 20:08:00 EST 2008
$

Hello All for all your feedback.

I tried "fpmurphy" suggestion in regards to ksh93 but it doesn't appear that this has been installed on our aix server. :frowning:

I did find a useful code that's doing a similar calculation to find "last Sunday" from an input date but it's written in Perl. Can someone assist me in converting it to a shell script? We also do not have Perl available here.

Thank you.

Humm, what version of AIX are you using?
What is the output of "print $KSH_VERSION" and "print ${.sh.version}"

AIX Version 5.2

$ print $KSH_VERSION

$ print ${.sh.version}
Version M-12/28/93e

I tried your example below and I get the following error:

printf "%T\n" "01012008"
ksh93: printf: T: unknown format specifier

Thank you.

You indeed have ksh93 installed but it is a very old version. That particular version was release at the end of July 1996. I believe that you need version h or better (released in 1999) for "%T" functionality.

Thanks fpmurphy for your quick reply....

Unfortunately, I'm not part of the Unix team system administrators and whichever versions they decide to install on the server is up to them.

So, is there any other options for me???

I'm thinking of a potential solution - can you let me know if this will work or not?

Question: For any given date, get previous Saturday's date.

Solution: Current date => convert to julian date (A)
(for example: 205 for today's date)

          Current date => get day of the week    \(B\)  
          \(for example: 3 for today's date\)

Previous Saturday = (A)-(B)-1 (since sunday = 0) <= (205 - 3)-1 = 201

Then convert julian date format back to calendar date format.....is there an easy way to do this?

Thanks.

I came up with a potential solution to find the previous Saturday's date based on current system date.

I made use of the existing logic to find previous day (posted on this forum) and based on the current day of the week, I keep looping and calling the previous_day() function until I get to previous Saturday's date.

Please let me know if you find any flaws with my script.

Thanks.

Code follows:

==========================================================
#!/usr/bin/ksh
#
####################################################
# Script to find previous Saturday's date based on current system date
####################################################

# Initialize variable
i=0

#
# Function area - copied from forum posting
#
previous_day()
{
# Convert variables into numeric fields
YEAR=`expr "$YEAR" + 0`
MONTH=`expr "$MONTH" + 0`
DAY=`expr "$DAY" - 1`

case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac

# If DAY less than 10, append zero in front
if [ ${DAY} -lt 10 ]
then
DAY=0${DAY}
fi

# If MONTH less than 10, append zero in front
if [ ${MONTH} -lt 10 ]
then
MONTH=0${MONTH}
fi

} # End of previous_day() function

# Obtain current system date
YEAR=`date '+%Y'`
MONTH=`date '+%m'`
DAY=`date '+%d'`

# Find current day of the week
wkday=`date '+%w'`
wkday=`expr "$wkday" + 1`

# Loop and call function previous_day until we get previous Saturday's date
while [ i -lt ${wkday} ]
do
# Call function to find previous day
previous_day

let i=i+1
done

# Return yesterday date
echo "$YEAR$MONTH$DAY"