#!/bin/sh
# http://www.linuxjournal.com/article/11025?page=0,1
export LC_TIME=C
usage()
{
cat <<!
USAGE:
${0##*/} WEEKDAY MONTH_NAME Day
${0##*/} WEEKDAY MONTH-DAY
${0##*/} WEEKDAY MONTH/DAY
!
}
if [ $# -ne 3 -a $# -ne 2 ]; then
usage
exit 1
fi
if [ $# -eq 3 ]; then
# GNU `date' accepts "Sep 25 2008"
fmt="$2 $3 %d"
else
# And also accepts "2011-9-25" or "9/25/2011"
case "$2" in
*-*)
fmt="%d-$2"
;;
*/*)
fmt="$2/%d"
;;
*)
echo "Uknown date: $2"
usage
exit 1
;;
esac
fi
case $(echo $1 | tr '[:upper:]' '[:lower:]' | cut -c1-3) in
mon) weekday=1 ;;
tue) weekday=2 ;;
wed) weekday=3 ;;
thu) weekday=4 ;;
fri) weekday=5 ;;
sat) weekday=6 ;;
sun) weekday=7 ;;
*)
echo "$1: Unknown weekday"
exit 1
;;
esac
MY_DATE_FMT="%Y/%m/%d"
MY_WDAY_FMT="%A"
MAX_TRY=128
y0=$(date +%Y)
i=0
found=0
while [ $i -lt $MAX_TRY ]; do
y=$((y0 - i))
str=$(printf "$fmt" $y)
if ! j=$(date -d "$str" +%u); then
# `date' will complain, so I keep quiet
exit 1
fi
if [ $j -eq $weekday ]; then
echo $(date -d "$str" +$MY_DATE_FMT) is \
$(date -d "$str" +$MY_WDAY_FMT)
found=1
fi
i=$((i + 1))
done
test $found -eq 0 && exit 1
exit 0
syntax highlighted by Code2HTML, v. 0.9.1