#!/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 [start_date in YYYY MM DD format] (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\n $0 90 2020 04 04 will return the date in 90 days from 4/4/2020" echo $# mSourceDir=/lsfprod/usr/bin/prod/Fidelity if [ $# -eq "0" ] #|| [ $# -ne 4 ] 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 # Process commandline parameters echo "Params $# $2 $3 $4" if [ $# -eq 1 ] then dom=`date '+%d'` dow=`date '+%w'` doy=`date '+%j'` year=`date '+%Y'` # 4 digit else year=$2 # 4 digit mnth=$3 dom=$4 echo dow=`$mSourceDir/dow.sh $2 $3 $4` echo doy=`$mSourceDir/doy.sh $2 $3 $4` dow=`$mSourceDir/dow.sh $2 $3 $4` doy=`$mSourceDir/doy.sh $2 $3 $4` fi 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 month=1 continue="yes" while test $continue = "yes" do subdays=`cal $month $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` 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" echo "$year $month $targetday" echo "$year$month$targetday" 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 month=1 continue="yes" while test $continue = "yes" do subdays=`cal $month $year | grep -v \^\$ | tail -1 | awk '{print $NF}'` 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" echo "$year $month $targetday" echo "$year$month$targetday" exit else ((month=month+1)) targetday=`echo "$targetday - $subdays" | bc` continue="yes" fi done