home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / MADD.PRG < prev    next >
Text File  |  1992-09-28  |  3KB  |  109 lines

  1. /*
  2.  * File......: MADD.PRG
  3.  * Author....: Jo W. French dba Practical Computing
  4.  * CIS ID....: 74731,1751
  5.  * Date......: $Date:   28 Sep 1992 00:39:04  $
  6.  * Revision..: $Revision:   1.3  $
  7.  * Log file..: $Logfile:   C:/nanfor/src/madd.prv  $
  8.  * 
  9.  * The functions contained herein are the original work of Jo W. French
  10.  * and are placed in the public domain.
  11.  *
  12.  * Modification history:
  13.  * ---------------------
  14.  *
  15.  * $Log:   C:/nanfor/src/madd.prv  $
  16.  * 
  17.  *    Rev 1.3   28 Sep 1992 00:39:04   GLENN
  18.  * Jo French cleaned up.
  19.  * 
  20.  *    Rev 1.2   15 Aug 1991 23:03:58   GLENN
  21.  * Forest Belt proofread/edited/cleaned up doc
  22.  * 
  23.  *    Rev 1.1   14 Jun 1991 19:52:14   GLENN
  24.  * Minor edit to file header
  25.  * 
  26.  *    Rev 1.0   01 Apr 1991 01:01:38   GLENN
  27.  * Nanforum Toolkit
  28.  *
  29.  */
  30.  
  31. /*  $DOC$
  32.  *  $FUNCNAME$
  33.  *     FT_MADD()
  34.  *  $CATEGORY$
  35.  *     Date/Time
  36.  *  $ONELINER$
  37.  *     Add or subtract months to/from a date
  38.  *  $SYNTAX$
  39.  *     FT_MADD( [ <dGivenDate> ], [ <nAddMonths> ], [ <lMakeEOM> ] )
  40.  *         -> dDate
  41.  *  $ARGUMENTS$
  42.  *     <dGivenDate> is any valid date in any date format. Defaults to
  43.  *     current system date if not supplied.
  44.  *
  45.  *     <nAddMonths> is the number of months to be added or subtracted.
  46.  *     Defaults to 0 if not supplied.
  47.  *
  48.  *     <lMakeEOM> is a logical variable indicating whether or not to
  49.  *     force the returned date to the last date of the month.  It only
  50.  *     affects the returned date if <dGivenDate> is an end-of-month date.
  51.  *  $RETURNS$
  52.  *     A date.
  53.  *  $DESCRIPTION$
  54.  *     FT_MADD() adds or subtracts months to/from a given date.
  55.  *
  56.  *     If MakeEOM is passed and dGivenDate is the last day of a month,
  57.  *     it will return the EOM of calculated month.  Otherwise it will
  58.  *     return the same day as the day of the passed date.
  59.  *  $EXAMPLES$
  60.  *     dDate := CTOD( "09/15/90" )
  61.  *     ? FT_MADD( dDate, 1 )        // 10/15/90
  62.  *     ? FT_MADD( dDate, -2 )       // 07/15/90
  63.  *
  64.  *     // force EOM
  65.  *     dDate := CTOD( "04/30/91" )
  66.  *     ? FT_MADD( dDate, 1 )        // 05/30/91
  67.  *     ? FT_MADD( dDate, 1, .T. )   // 05/31/91  <- forced EOM
  68.  *     ? FT_MADD( dDate, 2 )        // 06/30/91
  69.  *     ? FT_MADD( dDate, 2, .T. )   // 06/30/91  <- June only has 30 days
  70.  *     ? FT_MADD( dDate, 3 )        // 07/30/91
  71.  *     ? FT_MADD( dDate, 3, .T. )   // 07/31/91  <- forced EOM
  72.  *
  73.  *  $SEEALSO$
  74.  *     FT_DAYOFYR() FT_DAYTOBOW()
  75.  *  $END$
  76. */
  77.  
  78. FUNCTION FT_MADD( dGivenDate, nAddMonths, lMakeEOM)
  79.   LOCAL nAdjDay, dTemp, i
  80.  
  81.   IF(VALTYPE(dGivenDate) != 'D', dGivenDate := DATE(), )
  82.   IF(VALTYPE(nAddMonths) != 'N', nAddMonths := 0, )
  83.   IF(VALTYPE(lMakeEOM)   != 'L', lMakeEom := .F., )
  84.  
  85.   nAdjDay := DAY( dGivenDate ) - 1
  86.  
  87.   /* If givendate is end of month and lMakeEom, then force EOM.*/
  88.  
  89.   lMakeEom := ( lMakeEom .AND. dGivenDate ==  dGivenDate - nAdjDay + 31 - ;
  90.                 DAY( dGivenDate - nAdjDay + 31 ) )
  91.  
  92.   dTemp := dGivenDate - nAdjDay     // first of month
  93.  
  94.   /* Work with 1st of months.*/
  95.   FOR i := 1 TO ABS(nAddMonths)
  96.       dTemp += IF( nAddMonths > 0, 31, -1 )
  97.       dTemp += 1 - DAY( dTemp )
  98.   NEXT
  99.  
  100.   IF lMakeEom
  101.      dTemp += 31 - DAY( dTemp + 31 )
  102.   ELSE
  103.      dTemp := MIN( (dTemp + nAdjday), (dTemp += 31 - DAY( dTemp + 31 )))
  104.   ENDIF
  105.  
  106. RETURN dTemp
  107.  
  108.  
  109.