One of the long-awaited features of RPG IV (ILE RPG) is the ability to do date math. Now that RPG has date math, there are many ways to take advantage of it, even if your date fields aren't date data types. One way is by writing a generic date calculation routine, such as the one shown in 5. This routine will return a calculated date based on the values passed into it. There are four parameters to this routine:
One of the long-awaited features of RPG IV (ILE RPG) is the ability to do date math. Now that RPG has date math, there are many ways to take advantage of it, even if your date fields aren't date data types. One way is by writing a generic date calculation routine, such as the one shown in Figure 5. This routine will return a calculated date based on the values passed into it. There are four parameters to this routine:
1. A starting date
2. A duration value
3. A duration code
4. A return date
The first parameter (starting date) should be passed as an 8-byte character field in the format YYYYMMDD. The second parameter is the duration value that you want to use to increment the date. This routine supports both calculating dates later than the start date and calculating dates earlier than the start date. To calculate a date later than the start date, pass a positive value in the second parameter. To calculate a date earlier than the start date, pass in a negative value in the second parameter. The third parameter is a duration code. Pass a value of D to increment by days, M to increment by months, or Y to increment by years. The calculated date is returned through the fourth parameter as an 8-byte character field in YYYYMMDD format. If the routine determines that an invalid date has been passed in through the first parameter, it will pass back a date of all nines through the fourth parameter.
As with most ILE routines, there are several ways to implement this code. You could compile it as a standalone program with the Create Bound RPG Program (CRTBNDRPG) command. This would allow you to make external calls to it from your own programs, whether they're ILE programs or not.
Alternatively, you could compile this routine using the Create RPG Module (CRTRPGMOD) command, followed by either the Create Service Program (CRTSRVPGM) or the Create Program (CRTPGM) command. This would give you better performance by allowing you to bind the executable code into your own programs.
- Izzy Munoz and T.V.S. Murphy
Figure 5: RPG IV Date Calculation Routine
*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 D WorkDate S 10D DATFMT(*ISO) D InputDate S 8S 0 D Duration S 5 0 D DurCode S 1A D ReturnDate DS 8 D Year 4 D Month 2 D Day 2 * C *Entry PLIST C PARM InputDate C PARM Duration C PARM DurCode C PARM ReturnDate * C *ISO TEST(D) InputDate 99 * C IF *IN99 = *OFF C *ISO MOVE InputDate WorkDate * C SELECT C WHEN DurCode = 'D' C ADDDUR Duration:*D WorkDate C WHEN DurCode = 'M' C ADDDUR Duration:*M WorkDate C WHEN DurCode = 'Y' C ADDDUR Duration:*Y WorkDate C OTHER C ADDDUR Duration:*D WorkDate C ENDSL * C EXTRCT WorkDate:*Y Year C EXTRCT WorkDate:*M Month C EXTRCT WorkDate:*D Day * C ELSE * C MOVE *ALL'9' ReturnDate C ENDIF * C EVAL *INLR = *ON
LATEST COMMENTS
MC Press Online