home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mega CD-ROM 1
/
megacd_rom_1.zip
/
megacd_rom_1
/
CLIPPER
/
NFSRC.ZIP
/
MADD.PRG
< prev
next >
Wrap
Text File
|
1991-08-16
|
4KB
|
124 lines
/*
* File......: MADD.PRG
* Author....: Jo W. French dba Practical Computing
* CIS ID....: 74731,1751
* Date......: $Date: 15 Aug 1991 23:03:58 $
* Revision..: $Revision: 1.2 $
* Log file..: $Logfile: E:/nanfor/src/madd.prv $
*
* The functions contained herein are the original work of Jo W. French
* and are placed in the public domain.
*
* Modification history:
* ---------------------
*
* $Log: E:/nanfor/src/madd.prv $
*
* Rev 1.2 15 Aug 1991 23:03:58 GLENN
* Forest Belt proofread/edited/cleaned up doc
*
* Rev 1.1 14 Jun 1991 19:52:14 GLENN
* Minor edit to file header
*
* Rev 1.0 01 Apr 1991 01:01:38 GLENN
* Nanforum Toolkit
*
*/
/* $DOC$
* $FUNCNAME$
* FT_MADD()
* $CATEGORY$
* Date/Time
* $ONELINER$
* Add or subtract months to/from a date
* $SYNTAX$
* FT_MADD( [ <dGivenDate> ], [ <nAddMonths> ], [ <lMakeEOM> ] )
* -> dDate
* $ARGUMENTS$
* <dGivenDate> is any valid date in any date format. Defaults to
* current system date if not supplied.
*
* <nAddMonths> is the number of months to be added or subtracted.
* Defaults to 0 if not supplied.
*
* <lMakeEOM> is a logical variable indicating whether or not to
force the returned date to the last date of the month. It only
affects the returned date if <dGivenDate> is an end-of-month date.
* Defaults to false except for month of February.
* $RETURNS$
* A date.
* $DESCRIPTION$
* FT_MADD() adds or subtracts months to/from a given date.
*
* If MakeEOM is passed and dGivenDate is the last day of a month,
* if will return the EOM of calculated month. Otherwise it will
* return the same day as the day of the passed date.
* $EXAMPLES$
* dDate := CTOD( "09/15/90" )
* ? FT_MADD( dDate, 1 ) // 10/15/90
* ? FT_MADD( dDate, -2 ) // 07/15/90
*
* // force EOM
* dDate := CTOD( "04/30/91" )
* ? FT_MADD( dDate, 1 ) // 05/30/91
* ? FT_MADD( dDate, 1, .T. ) // 05/31/91 <- forced EOM
* ? FT_MADD( dDate, 2 ) // 06/30/91
* ? FT_MADD( dDate, 2, .T. ) // 06/30/91 <- June only has 30 days
* ? FT_MADD( dDate, 3 ) // 07/30/91
* ? FT_MADD( dDate, 3, .T. ) // 07/31/91 <- forced EOM
*
* $SEEALSO$
* FT_DAYOFYR() FT_DAYTOBOW()
* $END$
*/
FUNCTION FT_MADD(dGivenDate,nAddMonths, lMakeEOM)
LOCAL nAdd, nAddIncr, nMonth, dTemp, lEOMFlag
LOCAL dRetVal, aTemp, cDateFormat := SET(_SET_DATEFORMAT, "yyyy.mm.dd")
aTemp := {0,0,0}
dGivenDate := IIF(VALTYPE(dGivenDate) != 'D', DATE(), dGivenDate)
nAddMonths := IIF(VALTYPE(nAddMonths) != 'N', 0, nAddMonths)
lMakeEOM := IIF(VALTYPE(lMakeEOM) != 'L', .F., lMakeEOM)
nMonth := MONTH(dGivenDate)
lEOMFlag := .F.
IF lMakeEOM
/* Check if day entered is the end of the month entered.*/
dTemp := CTOD(STR(YEAR(dGivenDate) + IF(nMonth + 1 < 13, 0, 1 ), 4) + "." +;
STR(nMonth + IF( nMonth + 1 < 13, 1, - 11),2) + ".01") - 1
lEOMFlag := IF(DAY(dGivenDate) == DAY(dTemp), .T., lEOMFlag)
ENDIF
nAddIncr := nAddMonths % 12
IF nAddIncr == 0
aTemp[1] := YEAR(dGivenDate) + INT( nAddMonths / 12 )
aTemp[2] := nMonth
ELSEIF nAddIncr > 0
aTemp[1] := YEAR(dGivenDate) + IF( nMonth + nAddIncr < 13, 0, 1 ) +;
INT(nAddMonths / 12)
aTemp[2] := nMonth + IF( nMonth + nAddIncr < 13, nAddIncr, nAddIncr - 12)
ELSE
aTemp[1] := YEAR(dGivenDate) - IF( nMonth + nAddIncr > 0, 0, 1 ) -;
INT( ABS(nAddMonths) / 12 )
aTemp[2] := nMonth + IF( nMonth + nAddIncr > 0, nAddIncr, nAddIncr + 12 )
ENDIF
/* Determine end of month day for calculated year and month.*/
dTemp := CTOD( STR(aTemp[1]+IF(aTemp[2]=12,1,0),4) + "." + ;
STR(IF(aTemp[2]=12,1,aTemp[2]+1),2) + ".01") -1
IF lEOMFlag // Force end of month.
dRetVal := dTemp
ELSE // Assure dGivenDate day not > end of calculated month.
aTemp[3] := IF( DAY(dGivenDate) > DAY(dTemp), DAY(dTemp), DAY(dGivenDate) )
dRetval := CTOD(STR(aTemp[1],4) + "." +STR(aTemp[2],2) +"."+STR(aTemp[3],2))
ENDIF
SET(_SET_DATEFORMAT, cDateFormat)
RETURN dRetVal