Avatar of roosterup
 asked on

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

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?
IBM System i* AS400

Avatar of undefined
Last Comment

8/22/2022 - Mon
Gary Patterson, CISSP

In what language?
Gary Patterson, CISSP


- 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);
Gary Patterson, CISSP

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Gary Patterson, CISSP

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;