fromdate_k93



#!/usr/bin/ksh93
################################################################
function usagemsg_fromdate_k93 {
  print "
Program: fromdate_k93

This script calculates a date forward or backward for a
specified number of days from a given date.

Usage: ${1##*/} [-?] [-v] [-V] [-b beginDate] [-d days]

    Where:
          '-b' = Beginning date from which to start calculation.
                 Default value is today's date in the following
                 numeric format: 'YYYYMMDD'.
          '-d' = Number of days to add or subtract from today's 
                 date.  Value can be positive or negative, if 
                 negative, quote the value on the command line.
                 Default value is '-30' days
          '-v' = Verbose mode
          '-V' = Very Verbose Mode

Example: fromdate_k93 -b '20060320' -d '-30'

Author: Dana French (dfrench@mtxia.com) Copyright 2006

\"AutoContent\" enabled"
return 0
}
################################################################
function fromdate_k93
{
  typeset TRUE="0"   
  typeset FALSE="1"  
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset BEGINDATE=$( date +"%Y%m%d" )
  typeset DELTADAYS='-30'
  typeset VERSION="1.0"
  
  while getopts ":b#d#vV" OPTION
  do
      case "${OPTION}" in
          'b') BEGINDATE="${OPTARG}";;
          'd') DELTADAYS="${OPTARG}";;
          'v') VERBOSE="${TRUE}";; 
          'V') VERYVERB="${TRUE}";;
          '?') usagemsg_fromdate_k93 "${0}" && return 1;;
          ':') usagemsg_fromdate_k93 "${0}" && return 1;;
          '#') usagemsg_fromdate_k93 "${0}" && return 1;;
      esac
  done

  shift $(( ${OPTIND} - 1 ))
  
  (( VERBOSE == TRUE )) && print -u 2 -- "# fromdate Version.......: ${VERSION}"

  (( VERYVERB == TRUE )) && set -x

  typeset YEAR="${BEGINDATE:0:4}"
  typeset MON="${BEGINDATE:4:2}"
  typeset DAY="${BEGINDATE:6:2}"
  typeset DAYCNT="${DELTADAYS}"

  (( VERBOSE == TRUE )) && print -u 2 -- "# Beginning Date.........: ${BEGINDATE}"
  (( VERBOSE == TRUE )) && print -u 2 -- "# Beginning Year.........: ${YEAR}"
  (( VERBOSE == TRUE )) && print -u 2 -- "# Beginning Month........: ${MON}"
  (( VERBOSE == TRUE )) && print -u 2 -- "# Beginning Day of Month.: ${DAY}"
  (( VERBOSE == TRUE )) && print -u 2 -- "# Number of Days Delta...: ${DELTADAYS}"

  SMO=( nul jan feb mar apr may jun jul aug sep oct nov dec )
  DIM=(  0   31  28  31  30  31  30  31  31  30  31  30  31 )
  DIM[2]="28"
  ( ( (( ( YEAR % 4 )   == 0 )) &&
      (( ( YEAR % 100 ) != 0 )) ) ||
      (( ( YEAR % 400 ) == 0 )) ) &&
    DIM[2]="29"

  (( DAY = DAY + DAYCNT ))

################################################################
#### 
#### Determine if the specified year is a leap year, if so
#### modify the number of days in february.  This must be
#### done differently if subtracting days from the current
#### date than for adding days.  As a result two separate
#### loops are used depending upon whether days are being
#### added or subtracted from the current date.
#### 
#### The following is the loop for subtracting days from
#### the specified date.
#### 
################################################################

  while (( DAY <= 0 ))
  do
    (( --MON ))

    while (( MON <= 0 ))
    do
      (( MON = 12 - MON ))
      (( YEAR = YEAR - 1 ))
    done

    (( VERBOSE == TRUE )) && print -u 2 -- "# YEAR % 4   ............: $(( YEAR % 4 ))"
    (( VERBOSE == TRUE )) && print -u 2 -- "# YEAR % 100 ............: $(( YEAR % 100 ))"
    (( VERBOSE == TRUE )) && print -u 2 -- "# YEAR % 400 ............: $(( YEAR % 400 ))"

    DIM[2]="28"
    ( ( (( ( YEAR % 4 )   == 0 )) &&
        (( ( YEAR % 100 ) != 0 )) ) ||
        (( ( YEAR % 400 ) == 0 )) ) &&
      DIM[2]="29"

    (( DAY = ${DIM[MON]} + DAY ))

  done

################################################################
#### 
#### The following is the loop for adding days to
#### the specified date.
#### 
################################################################

  while (( DAY > ${DIM[MON]} ))
  do

    (( VERBOSE == TRUE )) && print -u 2 -- "# YEAR % 4   ............: $(( YEAR % 4 ))"
    (( VERBOSE == TRUE )) && print -u 2 -- "# YEAR % 100 ............: $(( YEAR % 100 ))"
    (( VERBOSE == TRUE )) && print -u 2 -- "# YEAR % 400 ............: $(( YEAR % 400 ))"

    DIM[2]="28"
    ( ( (( ( YEAR % 4 )   == 0 )) &&
        (( ( YEAR % 100 ) != 0 )) ) ||
        (( ( YEAR % 400 ) == 0 )) ) &&
        DIM[2]="29"

    (( DAY = DAY - ${DIM[MON]} ))
    (( ++MON ))

    while (( MON > 12 ))
    do
      (( MON = MON - 12 ))
      (( YEAR = YEAR + 1 ))
    done

  done

  (( VERBOSE == TRUE )) && print -u 2 -- "# Days in Feb ${YEAR}.......: ${DIM[2]}"

  typeset -Z4 YEAR4="${YEAR}"
  typeset -Z2 MON2="${MON}"
  typeset -Z2 DAY2="${DAY}"
  print -- "${YEAR4}${MON2}${DAY2}"

  return 0
}
################################################################

fromdate_k93 "${@}"