Free RPG - Calculate prorated amount of annual premium, account for leap years

roosterup used Ask the Experts™
I need to calculate a prorated amount of annual premium in free rpgle and account the number of days in leap years.

say a company's annual premium would be $1000 and they sign up on 11/01. I only need to charge them for 61 days of the yearly premium.

anyone have any date math examples for doing this?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Gary PattersonVP Technology / Senior Consultant

In what language?
Gary PattersonVP Technology / Senior Consultant


- Days from current date to end of current year.
values days(year(current_date) || '-12-31') - days (current_date);

- Days from DATE column
select days(year(current_date) || '-12-31') - days(PolicyEffectiveDate) as ProrateDays from sometable;

- Verify properly handles leap years, including special rules for years divisible by 100 and 400
values days('2016-03-01') - days ('2016-02-01')  --expect 29
values days('2017-01-01') - days ('2016-01-01')  -- normal leap year - expect 366
values days('1900-12-31') - days ('1899-12-31')  -- "100 rule" - expect 365
values days('2000-12-31') - days ('1999-12-31')  -- "400 rule" - expect 366

ILE RPG (didn't test, but hopefully you get the idea):

dcl-s endOfYear DATE(*ISO);
dcl-s effectiveDate DATE(*ISO) INZ('2017-03-17');
dcl-s proRateDays INT(5);

endOfYear = %date(%char(%subdt(%date() : *YEARS) :*ISO) || -12-31');
proRateDays = %diff( endOfYear : effectiveDate : *DAYS);
VP Technology / Senior Consultant
Note that you need to know if it is a leap year or not to calculate that daily rate:

In a leap year, dailyRate = annualRate/366;
In a normal year, dailyRate = annualRate/365;

You can generalize:

dcl-s endOfThisYear DATE(*ISO);
dcl-s endOfLastYear DATE(*ISO);
dcl-s annualRate DEC(7 :2) INZ(1234.56)
dcl-s dailyRate DEC(9 : 4)

-- Get last day of this year
endOfThisYear = %date(%char(%subdt(%date() : *YEARS) :*ISO) || -12-31');
-- Get last day of last year
endOfLastYear = endOfThisYear - %years(1);
-- Divide by 365 in normal year, and 366 in leap year
dailyRate = annualRate / %diff(endOfThisYear : endOfLastYear : *DAYS);
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Do you want it to be "61 days of a 365-day year because 2017 isn't a leap year" or "61 days of a 365.25-day year because that's a reasonable average over many years"? That is, do you want proration to be different for actual leap years or the same for every year?
Gary PattersonVP Technology / Senior Consultant

Since we're apparently dealing with some sort of insurance, it is possible that there is governing law that controls the calculation - and that could even vary by jurisdiction.  Might be tIme to email the legal team ...

Hi Roosterup, I see have your solution, but I would add a leap year checker like so:

 d Dec31PolYear s d
 d PolStrDate s d
 d LeapDay s 10
 d MultiPlier s 7 6  (7.6)
 d DaysLeftInYear s 3 0
 d OneYearPrimium s 10 2
 // Base Primium $10,0000 per year
     OneYearPrimium = 10000;
 // use %DIFF(Dec31CurYear : PolStrDate : *Days)
     Dec31PolYear = %Date(%CHAR(%SubDT(PolStrDate:*YEARS)) +
                                    '-12-31' : *iso);
     DaysLeftInYear = %DIFF(Dec31PolYear : PolStrDate : *Days) + 1;

   LeapDay = %CHAR(%SubDT(PolStrDate:*YEARS)) + '-02-29' ;
    test(ed) LeapDay;
    if Not %Error ;
       // It's a leap year
       MultiPlier = DaysLeftInYear / 366;
       // no Leap year
       MultiPlier = DaysLeftInYear / 365;

   OneYearPrimium = OneYearPrimium * MultiPler;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial