#!/bin/sh ####################################################################### # Program Name: adddays.sh # Purpose: calculate date ahead or behind by X days # Written by: J. David Schronce # Creation Date: 03/03/2000 ####################################################################### # 3/29/2007 - JDS - replace complex leapyear and DOM calculations # with call to 'cal' # #set -xv USAGE=" $0 USAGE:[-]number_of_days (where number_of_days between 365 and -365)\n\n Example: \n $0 -7 will return the date one week ago\n $0 90 will return the date in 90 days" if test $# = "0" then echo $USAGE exit elif test $1 -gt 365 then echo "parameter too large - max 365" exit elif test $1 -lt -365 then echo "parameter too large - max -365" exit else diffdays=`echo $1 | sed 's/+//g'` fi dom=`date '+%d'` dow=`date '+%w'` doy=`date '+%j'` year=`date '+%Y'` # 4 digit if test $diffdays -lt 0 then sign="-" diffdays=`echo "$diffdays * -1" | bc` if test $diffdays -ge $doy then year=`echo "$year - 1" | bc` # Test for leapyear if test `cal 2 $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` -eq "29" then yeardays=366 else yeardays=365 fi doy=`echo "$yeardays + $doy" | bc` fi targetday=`echo "$doy $sign $diffdays" | bc` else sign="+" # Test for leapyear if test `cal 2 $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` -eq "29" then yeardays=366 else yeardays=365 fi if test `echo "$diffdays + $doy" | bc` -gt $yeardays then year=`echo "$year + 1" | bc` echo "$sign doy=$doy" targetday=`echo "($diffdays + $doy) - $yeardays" | bc` else targetday=`echo "($diffdays + $doy)" | bc` fi fi # calculate current month # Test for leapyear #if test `cal 2 $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` -eq "29" #if test `/usr/local/bin/leapyear $year` = "Yes" #then # FEB=29 #else # FEB=28 #fi month=1 continue="yes" while test $continue = "yes" do subdays=`cal $month $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` # case $month in # 1|3|5|7|8|10|12) subdays=31;; # 2) subdays=$FEB;; # 4|6|9|11) subdays=30;; # esac if test $targetday -le $subdays then continue="no" case $month in ?) month="0$month";; esac case $targetday in ?) targetday="0$targetday";; esac echo "$month-$targetday-$year" exit else #((month=month+1)) month=`echo "$month + 1" | bc` targetday=`echo "$targetday - $subdays" | bc` continue="yes" fi done # Test for leapyear - see if Feb has 29 days if test `cal 2 $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` -eq "29" then yeardays=366 else yeardays=365 fi doy=`echo "$yeardays + $doy" | bc` fi targetday=`echo "$doy $sign $diffdays" | bc` else sign="+" # Test for leapyear - see if Feb has 29 days if test `cal 2 $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` -eq "29" then yeardays=366 else yeardays=365 fi if test `echo "$diffdays + $doy" | bc` -gt $yeardays then year=`echo "$year + 1" | bc` echo "$sign doy=$doy" targetday=`echo "($diffdays + $doy) - $yeardays" | bc` else targetday=`echo "($diffdays + $doy)" | bc` fi fi # calculate current month # Test for leapyear - see if Feb has 29 days # if test `cal 2 $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` -eq "29" #then # FEB=29 #else # FEB=28 #fi month=1 continue="yes" while test $continue = "yes" do subdays=`cal $month $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` # case $month in # 1|3|5|7|8|10|12) subdays=31;; # 2) subdays=$FEB;; # 4|6|9|11) subdays=30;; # esac if test $targetday -le $subdays then continue="no" case $month in ?) month="0$month";; esac case $targetday in ?) targetday="0$targetday";; esac echo "$month-$targetday-$year" exit else ((month=month+1)) targetday=`echo "$targetday - $subdays" | bc` continue="yes" fi done