Wanted: General Date Arithmatic

Andrew Beattie andrew at riddle.UUCP
Tue Sep 13 23:41:47 AEST 1988


in article <6006 at ihlpf.ATT.COM>, spock at ihlpf.ATT.COM (Weiss) says:
> 
> I would like to get my hands on (can you get hands on software?)
> some functions that can to arithmatic on dates. Things like:
> today + 2 weeks.  Anybody have anything like that?
>
This is my all-time favorite hack!  There follows a shell script
which will tell you things like what date is next Friday:

(I hope that the wizards think that this is sufficiently sneaky to justify
my cross posting.)
------ cut here ------
############################################################################
# whenis : find out date relative to today : Andrew Beattie 20/5/87        #
# mcvax!ukc!reading!riddle!andrew                                          #
# andrew at sphinx.co.uk                                                      #
############################################################################
# I place this program in the public domain but please keep this header    #
# on it - I want the fame and glory!                                       #
############################################################################

if [ $# = 0 ]
then
	echo 'whenis today'
	echo 'whenis [next|last|this] sunday|monday|tuesday|wednesday|th...'
	exit
fi

# store current daylight adjustment
light=`echo $TZ | cut -c4-`
# find day of week and put '-' in front of it
now=`date '+-%w'`
for i 
do
	case $i in
	today) now=0 ;;
	next) now="$now +7" ;;
	last) now="$now -7" ;;
	tomorrow) now="1" ;;
	yesterday) now="-1" ;;
	sunday) now="$now +0" ;;
	monday) now="$now +1" ;;
	tuesday) now="$now +2" ;;
	wednesday) now="$now +3" ;;
	thursday) now="$now +4" ;;
	friday) now="$now +5" ;;
	saturday) now="$now +6" ;;
	esac
done
# work out number of hours adjustment
adj=`echo "$light + ( -24 * ( $now ) )" |bc`
# now for the magic - adjust the time zone by the given number
# of hours and let date do the hard work.
TZ=GMT$adj date



More information about the Comp.unix.wizards mailing list