On RPG-LE Development

Postings on RPG-LE and i5/OS

Posts Tagged ‘RPGLE Data Structure

Formatting Current Date and Time using a Data Structure and RPG-IV

leave a comment »

It’s rare to write a program and not need some form of date and time. Often you need both. With a carefully crafted Data Structure, extracting the date and time becomes relatively painless. The bonus with this approach is that you get today’s date and time formatted in various ways. This proves useful for comparison operations, aging scenarios, and report page and screen headers.

Using An RPG-IV Approach

This approach uses RPG-IV based code and a basic data structure.

Shown below is a data structure defining a date/time format. Under TimeStamp the data has been subdivided where values for the whole timestamp can be obtained (tsTimeStamp) in addition to acquiring the following values.

Hours, minutes and second in field tsHHMMSS
Century, year, month and day in field tsYYYYMMDD [*ISO format]
Month, day, century and year in field tsMMDDYYYY [*USA format]

     D TimeStamp       DS
     D   tsTimeStamp                 14S 0
     D   tsHHMMSS                     6S 0 OVERLAY(tsTimeStamp:1)
     D     tsHour                     2S 0 OVERLAY(tsHHMMSS)
     D     tsMinute                   2S 0 OVERLAY(tsHHMMSS:*NEXT)
     D     tsSecond                   2S 0 OVERLAY(tsHHMMSS:*NEXT)
     D   tsYYYYMMDD                   8S 0 OVERLAY(tsTimeStamp:7)
     D     tsYear                     4S 0 OVERLAY(tsYYYYMMDD)
     D     tsMonth                    2S 0 OVERLAY(tsYYYYMMDD:*NEXT)
     D     tsDay                      2S 0 OVERLAY(tsYYYYMMDD:*NEXT)
     D   tsMMDDYYYY                   8S 0 OVERLAY(tsTimeStamp:7)
     D     tsMM                       2S 0 OVERLAY(tsMMDDYYYY)
     D     tsDD                       2S 0 OVERLAY(tsMMDDYYYY:*NEXT)
     D     tsYYYY                     4S 0 OVERLAY(tsMMDDYYYY:*NEXT)

     D DateNow_ISO     S               D   DATFMT(*ISO)
     D DateNow_USA     S               D   DATFMT(*USA)
     D BirthDate       S               D   DATFMT(*ISO)

Also shown are some stand-alone fields to contain the current date and time in *ISO and *USA format in addition to a date defining an arbitrary birthdate.

To populate the data structure with the current date and time, the following code is used.

C                   TIME                    tsTimeStamp

Although the data structure contains a properly formatted date and time, the problem is that we don’t know which format was used by the system to populate the data structure. System date formats can be established by third-party software packages or by setting the appropriate i5/OS system values. The next set of code shown below makes that determination. The code is biased to *ISO format.

      * What format is the timestamp in?  We want YYYYMMDD
     C     *ISO          TEST(D)                 tsYYYYMMDD             99
     C     *IN99         IFEQ      *OFF
     C                   MOVE      tsYYYYMMDD    DATENOW_ISO
      *
      * Date is not in *ISO format; check for *USA
     C                   ELSE
     C     *USA          TEST(D)                 tsMMDDYYYY             99
     C     *IN99         IFEQ      *OFF
     C                   MOVE      tsYYYYMMDD    DATENOW_USA
     C                   MOVE      DATENOW_USA   DATENOW_ISO
     C                   ENDIF
      *
     C                   ENDIF

With the system date obtained in an *ISO format, calculations like the one below which determine the number of years elapsed from a given date can be safely made without the system issuing an error.

     C                   MOVE      RDDOB         BIRTHDATE
     C     DATENOW_ISO   SUBDUR    BIRTHDATE     S2AGE:*Y

Redesigning the Solution Using an RPG-ILE Service Program


To see how this design is converted to use a more modern approach which uses a RPG-ILE service program, click here.

Written by Ben

2010/02/03 at 12:32 am