While skimming the updated RPG manual I found something that I had not seen before, I can initialize variables with special values. What do I mean by special values? Basically system type values that start with an asterisk. When I went back and looked at older documentation I found that this has been available since IBM i 6.1.
I no longer have to go to the Program Status Data Structure, PSDS, to get the user id of the person running the program, or to initialize a date field with the system date.
In this post I am going to give examples of how to use this in both fixed and all free format RPGLE.
The special values that can be used are:
- *EXTDFT - initialize an externally described data structure to the default values defined in its DDS.
- *NULL - Null.
- *USER - Current user profile.
- *JOB - Job date.
- *SYS - System date, time or timestamp.
The Job date is the date when the job started. If the job was started before midnight it will contain yesterday’s date. The system date is the current date as defined on your IBM i.
Let me start with the fixed format code:
01 D FixedUser S 10 inz(*user) 02 D FixedJobDate S D inz(*job) 03 D FixedSysDate S D inz(*sys) datfmt(*ymd-) 04 D FixedTimeStamp S Z inz(*sys) 05 D FixedTime S T inz(*sys) 06 D FixedTimeUsa S T inz(*sys) timfmt(*usa)
Line 1 uses the INZ(*USER) to initialize the field with the user id of the person.
Line 2 by using INZ(*JOB) the variable is initialized with the job date, like using UDATE or *DATE.
Lines 3 – 6 are all initialized using INZ(*SYS) which is the system date (line 3), time (lines 5 and 6), and time stamp (line 4). On lines 3 and 6 I show how use the DATFMT and TIMFMT to change the format, the way the date or time is presented.
Below are the values each variable contained when I ran my test:
And now for the free format code:
01 dcl-s FreeUser char(10) inz(*user) ; 02 dcl-s FreeJobDate date inz(*job) ; 03 dcl-s FreeSystemDate date(*usa) inz(*sys) ; 04 dcl-s FreeTimeStamp timestamp inz(*sys) ; 05 dcl-s FreeTime time inz(*sys) ; 06 dcl-s FreeTimeUsa time(*usa) inz(*sys) ;
The variables are defined to be the same as the fixed format ones.
I did learn that the DATFMT and TIMFMT are not used in free format definitions. When you define a variable as a date or time you follow the variable type word, DATE or TIME with the format you want, see lines 3 and 6.
The table below shows the values of the variables from my testing:
For more information on this you can consult the IBM documentation here.