Data-Manipulation Operations Using Free Format, Part Three

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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.

Data-Manipulation Operations Using Free Format, Part Three - Figure 1

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.

Data-Manipulation Operations Using Free Format, Part Three - Figure 2

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!

JIM MARTIN

Jim Martin holds a BS degree in mathematics and an MS in computer science. For 26 years, he was employed by IBM, where he wrote RPG applications for customers and worked in the programming laboratory as a programmer on portions of CPF and the OS/400 operating system. After leaving IBM, Jim took post-graduate work in computer science and performed RPG training. He is an IBM-certified RPG IV developer and author of multiple bestselling editions of Free-Format RPG IV, which, since the book's initial publication in 2005, have taught thousands of RPG IV programmers how to be successful with the free-format coding style.


MC Press books written by Jim Martin available now on the MC Press Bookstore.

Free-Format RPG IV: Third Edition Free-Format RPG IV: Third Edition
Improve productivity, readability, and program maintenance with the free-format style of programming in RPG IV.
List Price $59.95

Now On Sale

Free-Format RPG IV: Second Edition Free-Format RPG IV: Second Edition
>Make the transition from coding in fixed-format RPG to free format.
List Price $59.95

Now On Sale

Functions in Free-Format RPG IV Functions in Free-Format RPG IV
Here’s the ultimate guide to writing RPG IV programs with functions in the free-format style.
List Price $59.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: