In my experience, most people have standardized on eight-digit zoned numeric fields in their database files for date fields. The format is typically YYYYMMDD.
This standard obviously came from the Y2K era and supports RPG III as well as RPG IV. RPG III does not recognize date data type values in database files, so using true date data types would not have been a viable alternative. RPG III only allows you to convert date fields to character, which is pretty useless.
But in RPG IV, we can take advantage of the date arithmetic operation codes, such as ADDDUR, SUBDUR, and EXTRCT. But these opcodes only work with true date data type fields. Therefore, we have to convert any eight-digit field that contains date information into a true date data type variable.
RPG IV supports this type of conversion via the MOVE opcode. For example, if our database file contains an eight-digit zoned numeric field named ORDDTE, we can convert that field's value into a real date field using the following RPG IV syntax:
D DueDate S D DatFmt(*ISO)
C *ISO MOVE ORDDTE ORDERDATE
C ADDDUR 30:*DAYS DUEDATE
C *ISO MOVE DUEDATE DUEDTE
In this example, the ORDDTE field is moved into the ORDERDATE field. The MOVE opcode does the conversion for us. The value in Factor 1 identifies the format of the ORDDTE field. Then, 30 days are added to the ORDERDATE, calculating the DUEDATE. Then, DUEDATE is converted into an eight-digit zoned numeric value in YYYYMMDD format. The *ISO in Factor 1 of the second MOVE opcode controls the target date format of the numeric field.
The MOVE opcode is a powerful tool. The EVAL opcode, which supplants the MOVE in free-format, does not offer nearly the capabilities of MOVE. For example, you cannot copy a date field to a numeric or character target variable using EVAL.
This restriction causes programmers to use certain built-in functions to perform the conversion. For example, to convert a date field to character format, %CHAR would be used as follows:
D szDUE S 10A
C eval szDue = %char(DueDate:*USA)
This example converts the date field DUEDATE into MM/DD/YYYY format and copies it to the szDUE field. Again, a date field in a character field is rarely needed (except for output purposes).
To convert a date field into a numeric field, other built-in functions are needed--particularly, %INT.
D DUEDTE S 8S 0
C eval DueDte = %int(%char(DueDate:*ISO0))
In this example, the %INT built-in function converts the character value to numeric. In order for the date to appear as a character string without the embedded date edit symbols, the %CHAR built-in function is used with the optional second parameter specified as %ISO0. This date formatting code has a trailing zero, which indicates that date separators will not be inserted into the result. So, for example, instead of a value of 2004-09-30, the result is 20040930.
If you are on i5/OS V5R3, you can use the %DEC built-in function to accomplish the same thing:
D DUEDTE S 8S 0
C eval DueDte = %dec(DueDate:*ISO)
To convert the value back to a date field, use the %DATE built-in function:
D DUEDTE S 8S 0
C eval DueDate = %date(DueDte:*ISO)
If you have the RPG xTools, you can use the DateToDec() and DecToDate() procedures to accomplish the same task. The benefits of the xTools in this situation is that they work on OS/400 V4R5 and later, whereas some other techniques work only on V5R1 and some work only on V5R2, while the %DEC example doesn't work until V5R3. We get to try to keep it straight.
If you have the xTools, here is what you would need to do:
D DueDate S D DatFmt(*ISO)
D DUEDTE S 8S 0
** Convert Date to Decimal
C eval DueDte = DateToDec(DueDate:*ISO)
** Convert Decimal to Date
C eval DueDate = DecToDate(DueDate:*ISO)
The syntax is marginally cleaner, and as I said, it works on V4R5 and later, whereas the %int(%char()) technique requires V5R2 or later.
Also note that the date format code does not need the trailing zero because you're converting to decimal, and the procedure is smart enough to know you don't need the separators.
Use any of these techniques to convert between date fields and decimal fields: Read from your database, convert to date, do the date math, and then convert the results back to numeric fields and write them to your database files. It ain't rocket science.
Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online