RPG IV provides a rich set of built-in functions, many of which target data manipulation tasks. Read Parts One and Two.
Editor's Note: This article is excerpted from chapter 8 of Free-Format RPG IV: Third Edition, by Jim Martin.
In this excerpt series, we explore those options, which include assignment statements, a large stable of built-in functions, and operations for converting dates and times.
Date and Time Operations
Date operations such as Adddur and Subdur came to RPG IV with the beginning of the date, time, and timestamp data types. Free-format RPG IV doesn’t support these operation codes. Instead, new built-in functions are available to provide this functionality.
Converting Numbers to Date and Time
To convert dates in a character or numeric data type to a date data type, you use the %Date built-in function. The function’s first parameter is the character or numeric data (presumably a date) to be converted. If the data in parameter 1 is not correct, an exception occurs. The second parameter is the format of the input data. There is a limited list of the possible formats—all special values beginning with an asterisk (*). The format parameter is optional; if you don’t specify it, it defaults to *ISO or the date format specified on the H control specification. The %Date function returns the date value as a date data type. To retrieve the system date, specify %Date with no parameters.
To convert time values in character or numeric data to the time data type, you use the %Time built-in function. The function’s first parameter is the character or numeric data to be converted. If the data in this parameter is not correct, an exception occurs. The second parameter is the format of the input data. As with %Date, a limited list of possible time formats is available; again, all are special values beginning with an asterisk. The format parameter is optional; when not specified, it defaults to *ISO or the time format specified on the H control specification. The %Time function returns the time as a time data type. To retrieve the system time, specify %Time with no parameters.
Although infrequently used, the %Timestamp built-in function is available to convert a character or numeric field to a timestamp data type. To retrieve the current system timestamp, specify %Timestamp with no parameters.
Converting Date and Time to Character or Decimal
Two built-in functions that you learned about earlier are also available to perform date and time conversions. You can use the %Char function to convert a date or time data type to a character string. In this scenario, the function’s first parameter is the date or time variable. The second parameter is the desired format, from the list of possible special values, of the date or time in the character string. The output will include date or time separator characters unless you specify a 0 after the format.
You can use the %Dec built-in function to convert a date or time data type to a decimal number. The function’s first parameter is the date or time variable.
The second parameter is the desired format, from the list of possible special values, of the date or time in the numeric output. (The *USA option is not allowed for time conversion.) Listing 8-15 shows additional examples of date and time conversion.
Listing 8-15: Examples of date and time conversions
Date and Time Arithmetic
To perform date, time, and timestamp operations, you use date or time variables and one or more of several built-in functions. For date operations, you use %Days, %Months, and %Years. For time operations, you use %Hours, %Minutes, %Seconds, and %Mseconds. Each of these built-in functions has one parameter: an unsigned integer representing the duration for the function.
You can use another built-in function, %Diff, with date, time, or timestamp data types to calculate the difference between two date, time, or timestamp values. Timestamp operations can use any of the above built-in functions.
The %Subdt built-in function retrieves a desired portion of a date, time, or timestamp variable. The first parameter is the date, time, or timestamp field. The second parameter is a special value that identifies which portion of the field you want to extract: *Months, *Days, or *Years for a date data type; *Hours, *Minutes, or *Seconds for a time data type; and any of the preceding values or the value *Mseconds (microseconds) for a timestamp data type.
Listing 8-16 shows examples of date arithmetic using the date and time built-in functions.
Listing 8-16: Using date arithmetic with built-in functions
Stay tuned for the next excerpt from Jim's great Free Format RPG IV Book! Can't wait? You can pick up Jim Martin's book, Free-Format RPG IV: Third Edition at the MC Press Bookstore Today!
LATEST COMMENTS
MC Press Online