Re: LEAP YEAR PROBLEM
Posted in 1996
Here is a possible solution to the year subtraction problem. Note I said 'possible'. There is probably an easier way, or even more efficient one, but, then, it wouldn't be this one. HTH, --v-- SNIP --v-- SNIP --v-- SNIP --v-- SNIP --v-- SNIP --v-- SNIP --v-- # subyear.4gl - test year subtraction bug for leap year MAIN DEFINE current_day, one_year, three_year, five_year DATE IF NUM_ARGS() <> 1 THEN LET current_day = TODAY ELSE LET current_day = ARG_VAL(1) END IF LET one_year = SubtractYearsFromDate(current_day, 1) DISPLAY current_day, " - 1 year = ", one_year LET three_year = SubtractYearsFromDate(current_day, 3) DISPLAY current_day, " - 3 years = ", three_year LET five_year = SubtractYearsFromDate(current_day, 5) DISPLAY current_day, " - 5 years = ", five_year END MAIN FUNCTION SubtractYearsFromDate(CurrentDate, NumberOfYears) DEFINE CurrentDate DATE, NumberOfYears SMALLINT, LastDay SMALLINT, NewDate DATE # Get date of first day of month LET NewDate = MDY( MONTH ( CurrentDate ), 1, YEAR( CurrentDate ) - NumberOfYears ) # get last day of month LET LastDay = DAY( ( NewDate - DAY( NewDate ) UNITS DAY + 1 UNITS DAY ) + 1 UNITS MONTH - 1 UNITS DAY ) # if leap year and on 29th, set to 28th IF DAY( CurrentDate ) > LastDay THEN LET NewDate = NewDate + LastDay - 1 ELSE LET NewDate = NewDate + DAY( CurrentDate ) - 1 END IF RETURN NewDate END FUNCTION --^-- SNIP --^-- SNIP --^-- SNIP --^-- SNIP --^-- SNIP --^-- SNIP --^-- Robert Minter Data Systems Support \\\\\\_/// Senior Software Engineer A Client Technologies Company ( _ _ ) E-Mail: rob@dssmktg.com Tel: 714.771.0454 (| ^ |) #include <disclaimer.h> Fax: 714.771.3028 \\`-'/ De Colores - Emmaus OC-13 SURF'S UP \\_/