Procedures — Initial guide to creating them in RPGLE
Download the source:
In this example we will provide an example of how to get date values in a multitude of formats.
You may pass it a date — or — don’t pass and the procedure will use current date.
Note: The definition of the date types can be set to a specific format.
Example: [cc lang=”php”]d WorkIsodate s d datfmt(*ISO)[/cc]
We will start with the procedure code, this will include both the source for the procedure and a copy book of the PR.
Placing the datastructures/arrays in the copy book, make them available to the main program when calling the procedure.
We will not be discussing service programs here.
create your source member to use for storage of code
[cc lang=”c++”]
crtsrcpf jjflib/qprcsrc rcdlen(112)
[/cc]
Procedure first (getdate):
[cc lang=”php”]
H NOMAIN EXPROPTS(*RESDECPOS)
* PROGRAM – getdate
* PURPOSE – Subprocedure to format date into multiple formats
* WRITTEN – September 05 2007
* AUTHOR – jamie flanary
* Parameters
* Indate Char 8 input
* Insubdays dec 6 input
* Inweekends Char 1 input (Y=include weekends)
* Inholidays Char 1 input (Y=include holidays)
*
* if inweekends and/or inholidays not passed then default to “N”
* Indicator Usage
* None
* The getdatE_CP copy member should be used by programs that call this subprocedure
* to obtain the procedure prototype fields.
/copy qprcsrc,getdate_cp
d a s 5 0
d y s 5 0
d m s 5 0
d char10 s 10
d Dateinerror s 1 inz(‘N’)
d DayCount s 6 0 inz
d DayOf s 1 0 inz
d DayOfweek s 1 0 inz
d decimal6 s 6 0 inz
d f1 s 7 0
d f2 s 7 0
d f3 s 7 0
d f4 s 7 0
d f5 s 7 0
d formatBack s 65535A varying
d holidaycount s 6 0
d indate s 10a
d Lilian s 10i 0
d NewDate s 52a
d outdate s 8
d work6 s 6 0
d work7 s 7 0
d work8 s 8 0
d workIso s D
d workIso0 s 8 0
d workdate s 8
d workdays s 6 0
d workholidays s 1
d workweekends s 1
*
* Begin Procedure
P getdate B export
* Procedure Interface
d getdate pi 512
d indate 8 const options(*nopass)
d indays 6 0 const options(*nopass)
d inweekends 1 const options(*nopass)
d inholidays 1 const options(*nopass)
/free
Exec SQL set option datfmt= *iso;
//all parms are optional so check to see what was passed
if %parms >= 1;
workdate = indate;
else;
clear workdate;
endif;
if %parms >= 2;
workdays = indays;
else;
clear workdays;
endif;
if %parms >= 3;
workweekends = inweekends;
else;
workweekends = ‘N’;
endif;
if %parms >= 4;
workholidays = inholidays;
else;
workholidays = ‘N’;
endif;
exsr $checkdate;
// if valid date passed in (or *blanks) then process
if dateinerror = ‘N’;
if workdays <> *zeros;
exsr $checkdatemath;
endif;
exsr $formatdate;
endif;
dateds.error = ‘N’;
if dateinerror = ‘Y’;
dateds.error = ‘Y’;
endif;
return dateDS;
//——————————————————–
// $checkdate – figure out what format the date is in
// if format found then validate.
//——————————————————–
begsr $checkdate;
// make sure that passed in parameter contains all digits (or blanks)
if %check(‘ 0123456789 ‘: workdate) = 0;
// get the length of workdate this will help determine
// what format the date is in if blank then workdate
// will be Today. ==> get date into *ISO format for the
// rest of the process.
reset dateinerror;
workiso = *loval;
select;
when workdate = *blanks;
workiso = %date();
when %len(%trim(workdate)) = 6;
// *MDY or *YMD or *DMY
work6 = %dec(workdate:6:0);
test(de) *mdy work6;
if not%error;
WorkIso = %date(work6:*mdy);
else;
test(de) *ymd work6;
if not%error;
WorkIso = %date(work6:*ymd);
else;
test(de) *dmy work6;
if not%error;
WorkIso = %date(work6:*dmy);
else;
// date is 6 in length but not valid
dateinerror = ‘Y’;
endif;
endif;
endif;
when %len(%trim(workdate)) = 7;
// *cymd
work7 = %dec(workdate:7:0);
test(de) *cymd work7;
if not%error;
WorkIso = %date(work7:*cymd);
else;
dateinerror = ‘Y’;
endif;
when %len(%trim(workdate)) = 8;
// *iso
work8 = %dec(workdate:8:0);
test(de) *iso work8;
if not%error;
WorkIso = %date(work8:*iso);
else;
dateinerror = ‘Y’;
endif;
other;
// date not in known format……
dateinerror = ‘Y’;
endsl;
// date contains characters ……
else;
dateinerror = ‘Y’;
endif;
endsr;
//——————————————————–
// $checkdatemath – check to see if user wants to add/sub
// days from passed in date also check
// to see if user wants to include
// weekends in date math.
//——————————————————–
begsr $checkdatemath;
clear daycount;
dou daycount = %abs(workdays);
select;
when workdays < *zeros;
workiso -= %days(1);
when workdays > *zeros;
workiso += %days(1);
endsl;
exec sql
values dayofweek(:workiso) into :dayofweek;
clear holidaycount;
workiso0 = %dec(workiso:*iso);
exec sql select count(*)
into :holidaycount
from HOLIDAYS
where HOLDATE = : workiso;
if workweekends = ‘Y’ or
workweekends = ‘N’ and dayofweek <> 1 and
dayofweek <> 7;
if workholidays = ‘Y’ or
workholidays = ‘N’ and holidaycount = *zeros;
daycount +=1;
endif;
endif;
enddo;
endsr;
//——————————————————–
// $Formatdate – format the date into multiple formats
//——————————————————–
begsr $Formatdate;
// okay start the manipulation
dateds.iso = workiso;
// date 19390801 will validate as a “proper”
// date but when you try to move to *MDY well
// very bad things will happen.
if %subdt(workiso:*years) < 1940;
char10 = %char(workiso:*iso);
decimal6 = %dec(%subst(char10:6:2) +
%subst(char10:9:2) +
%subst(char10:3:2):6:0);
dateds.mdy = %date(decimal6:*mdy);
else;
dateds.mdy = workiso;
endif;
dateds.usa = workiso;
dateds.iso0 = %dec(workiso:*iso);
dateds.mdy0 = %dec(dateds.mdy:*mdy);
dateds.usa0 = %dec(workiso:*usa);
dateds.cymd0 = %dec(workiso:*cymd);
// julian date - same date rules apply here
// The range for a 2-digit year is 1940-2039
// The range for a 3-digit year is 1900-2899
// The range for a 4-digit year is 0001-9999
if %subdt(workiso:*years) < 1940;
// 14 - months / 12
a = (14 - %subdt(workiso:*months)) / 12;
if a < 1;
a = *zeros;
endif;
// year + 4800 - a
y = (%subdt(workiso:*years) + 4800) - a;
// month + 12(a) - 3
m = (%subdt(workiso:*months) + (12*a) - 3);
f1 = (153*m + 2) / 5;
f2 = 365*y;
f3 = y/4;
f4 = y/100;
f5 = y/400;
dateds.jul0 =
%subdt(workiso:*days) +
f1 +
f2 +
f3 -
f4 +
f5 -
32045;
else;
exec sql
values julian_day(:dateds.iso) into :dateds.jul0 ;
endif;
// get day of week
exec sql
values dayofweek(:dateds.iso) into :DayOf;
dateds.dayname = Arr_Dayname(DayOf);
// month name
dateds.monthname = Arr_monname(%subdt(dateds.iso:*months));
// last day of current month
exec sql
set :dateds.lstdaycur = char(last_day(:dateds.iso), ISO);
// last day of current month decimal 8
dateds.lstdaycuriso = %dec(%Date(dateds.lstdaycur): *Iso);
// date 19390801 will validate as a "proper"
// date but when you try to move to *MDY well
// very bad things will happen.
if %subdt(%date(dateds.lstdaycur:*iso):*years) < 1940;
char10 = %char(%date(dateds.lstdaycur));
decimal6 = %dec(%subst(char10:6:2) +
%subst(char10:9:2) +
%subst(char10:3:2):6:0);
dateds.lstdaycurmdy = %dec(%date(decimal6:*mdy):*mdy);
else;
dateds.lstdaycurmdy = %dec(%date(dateds.lstdaycur): *mdy);
endif;
dateds.lstdaycurcymd = %dec(%Date(dateds.lstdaycur): *cymd);
// first day of current month
workiso = dateds.iso - %days(%subdt(dateds.iso:*d)-1);
dateds.fstdaycur = %char(workiso);
// first day of current month decimal 8
dateds.fstdaycuriso = %dec(%Date(dateds.fstdaycur): *Iso);
if %subdt(%date(dateds.fstdaycur:*iso):*years) < 1940;
char10 = %char(%date(dateds.fstdaycur));
decimal6 = %dec(%subst(char10:6:2) +
%subst(char10:9:2) +
%subst(char10:3:2):6:0);
dateds.fstdaycurmdy = %dec(%date(decimal6:*mdy):*mdy);
else;
dateds.fstdaycurmdy = %dec(%Date(dateds.fstdaycur): *mdy);
endif;
dateds.fstdaycurcymd = %dec(%Date(dateds.fstdaycur): *cymd);
// last day of previous month
dateds.workiso = dateds.iso - %months(1);
exec sql
set :dateds.lstdaylst = char(last_day(:dateds.workiso), ISO);
dateds.lstdaylstiso = %dec(%Date(dateds.lstdaylst): *Iso);
if %subdt(%date(dateds.lstdaylst:*iso):*years) < 1940;
char10 = %char(%date(dateds.lstdaylst));
decimal6 = %dec(%subst(char10:6:2) +
%subst(char10:9:2) +
%subst(char10:3:2):6:0);
dateds.lstdaylstmdy = %dec(%date(decimal6:*mdy):*mdy);
else;
dateds.lstdaylstmdy = %dec(%date(dateds.lstdaylst): *mdy);
endif;
dateds.lstdaylstcymd = %dec(%Date(dateds.lstdaylst): *cymd);
// first day of previous month
dateds.workiso = dateds.iso - %months(1);
workiso = dateds.workiso - %days(%subdt(dateds.workiso:*d)-1);
dateds.fstdaylst = %char(workiso);
dateds.fstdaylstiso = %dec(%Date(dateds.fstdaylst): *Iso);
if %subdt(%date(dateds.fstdaylst:*iso):*years) < 1940;
char10 = %char(%date(dateds.fstdaylst));
decimal6 = %dec(%subst(char10:6:2) +
%subst(char10:9:2) +
%subst(char10:3:2):6:0);
dateds.fstdaylstmdy = %dec(%date(decimal6:*mdy):*mdy);
else;
dateds.fstdaylstmdy = %dec(%Date(dateds.fstdaylst): *mdy);
endif;
dateds.fstdaylstcymd = %dec(%Date(dateds.fstdaylst): *cymd);
// build a date string
CEEDAYS(%char(dateds.iso): 'YYYY-MM-DD': lilian: *omit);
CEEDATE(Lilian: 'DD Mmm YYYY': NewDate: *omit);
dateds.string1 = newdate;
// this ===> 24 Jun 2007
formatBack = ‘Wwwwwwwwwz, DD Mmm YYYY’;
CEEDATE(Lilian: Formatback : NewDate: *omit);
dateds.string2 = newdate;
// this ===> Wednesday, 27 Jun 2007
formatBack = ‘Wwwwwwwwwz, DD Mmmmmmmmmz YYYY’;
CEEDATE(Lilian: Formatback : NewDate: *omit);
// this ===> Wednesday, 27 June 2007
dateds.string3 = newdate;
// get timestamp
dateds.time = %time();
exec sql
values current_Timestamp into :Dateds.timestamp;
endsr;
//——————————————————–
/end-free
p getdate E
[/cc]
Copy Book Next (getdate_cp):
[cc lang=”php”]
* What is returned
* EVAL date
* DATEDS.ERROR = ‘N’ 1
* DATEDS.ISO = ‘2007-08-01′ 2-11
* DATEDS.MDY = ’08/01/07′ 12-19
* DATEDS.USA = ’08/01/2007’ 20-29
* DATEDS.ISO0 = 20070801. 30-37
* DATEDS.MDY0 = 080107. 38-43
* DATEDS.USA0 = 08012007. 44-51
* DATEDS.JUL0 = 2454314. 52-58
* DATEDS.DAYNAME = ‘Wednesday’ 59-67
* DATEDS.MONTHNAME = ‘August ‘ 68-76
* DATEDS.LSTDAYCUR = ‘2007-08-31’ 77-86
* DATEDS.LSTDAYCURISO = 20070831. 87-94
* DATEDS.LSTDAYCURMDY = 083107. 95-100
* DATEDS.LSTDAYCURCYMD = 1070831. 101-107
* DATEDS.FSTDAYCUR = ‘2007-08-01’ 108-117
* DATEDS.FSTDAYCURISO = 20070801. 118-125
* DATEDS.FSTDAYCURMDY = 080107. 126-131
* DATE.DSFSTDAYCURCYMD = 1070801. 132-138
*
* DATEDS.LSTDAYLST = ‘2007-07-31’ 139-148
* DATEDS.LSTDAYLSTISO = 20070731. 149-156
* DATEDS.LSTDAYLSTMDY = 073107. 157-162
* DATEDS.LSTDAYLSTCYMD = 1070731. 163-169
* DATEDS.FSTDAYLST = ‘2007-07-01’ 170-179
* DATEDS.FSTDAYLSTISO = 20070701. 180-187
* DATEDS.FSTDAYLSTMDY = 070107. 188-193
* DATEDS.FSTDAYLSTCYMD = 1070701. 194-200
*
* DATEDS.STRING1 = ’01 Aug 2007 ‘ 201-230
* DATEDS.STRING2 = ‘Wednesday, 01 Aug 2007 ‘ 231-260
* DATEDS.STRING3 = ‘Wednesday, 01 August 2007 ‘ 261-290
* DATEDS.TIME = ‘16.05.03’ 291-298
* DATEDS.TIMESTAMP = ‘2007-08-01-16.05.03.000000’ 299-324
* DATEDS.WORKISO = ‘2007-07-01’ 325-334
* DATEDS.CYMD0 = 1070701 335-341
* getdat Format date into multiple formats
d getdate pr 512
d Indate 8 const options(*nopass)
d Indays 6 0 const options(*nopass)
d Inweekends 1 const options(*nopass)
d Inholidays 1 const options(*nopass)
// the multiple formats of the date
d dateDS ds qualified inz
d error 1 inz(‘N’)
d iso d datfmt(*iso)
d mdy d datfmt(*mdy)
d usa d datfmt(*usa)
d iso0 8 0
d mdy0 6 0
d usa0 8 0
d jul0 7 0
d dayname 9
d monthname 9
d lstdaycur 10
d lstdaycuriso 8 0
d lstdaycurmdy 6 0
d lstdaycurcymd 7 0
d fstdaycur 10
d fstdaycuriso 8 0
d fstdaycurmdy 6 0
d fstdaycurcymd 7 0
d lstdaylst 10
d lstdaylstiso 8 0
d lstdaylstmdy 6 0
d lstdaylstcymd 7 0
d fstdaylst 10
d fstdaylstiso 8 0
d fstdaylstmdy 6 0
d fstdaylstcymd 7 0
d string1 30
d string2 30
d string3 30
d time t
d timestamp z
d workiso d datfmt(*iso)
d cymd0 7 0
d ds_Dayname ds
d 9 Inz(‘Sunday’)
d 9 Inz(‘Monday’)
d 9 Inz(‘Tuesday’)
d 9 Inz(‘Wednesday’)
d 9 Inz(‘Thursday’)
d 9 Inz(‘Friday’)
d 9 Inz(‘Saturday’)
d arr_Dayname 9 Dim(8) OverLay(ds_Dayname)
d ds_monname ds
d 9 Inz(‘January’)
d 9 Inz(‘February’)
d 9 Inz(‘March’)
d 9 Inz(‘April’)
d 9 Inz(‘May’)
d 9 Inz(‘June’)
d 9 Inz(‘July’)
d 9 Inz(‘August’)
d 9 Inz(‘September’)
d 9 Inz(‘October’)
d 9 Inz(‘November’)
d 9 Inz(‘December’)
d arr_monname 9 Dim(12) OverLay(ds_monname)
d CEEDAYS pr opdesc
d InputDate 65535A const options(*varsize)
d picture 65535A const options(*varsize)
d Lilian 10i 0
d Feedback 12a options(*omit)
d CEEDATE pr opdesc
d Lilian 10i 0
d picture 65535A const options(*varsize)
d OutputDate 65535A const options(*varsize)
d Feedback 12a options(*omit)
[/cc]
Once I have the code working and I compile it using: Create SQL ILE RPG Object (CRTSQLRPGI)
[cc lang=”java”]
CRTSQLRPGI OBJ(JJFLIB/GETDATE) SRCFILE(JJFLIB/QPRCSRC)
OBJTYPE(*MODULE) DBGVIEW(*SOURCE) DBGENCKEY(*NONE)
[/cc]
Now we meed to add the procedure to a binding directory.
[cc lang=”php”]
ADDBNDDIRE BNDDIR(RBSLIB/UTILITIES) OBJ((GETDATE *MODULE))
[/cc]
Now we are ready to test with a program (getdatetst):
[cc lang=”php”]
H DFTACTGRP(*NO) OPTION(*SRCSTMT: *NODEBUGIO) BNDDIR(‘UTILITIES’)
/copy qprcsrc,GETDATE_CP
d MyDate s 8 Inz
*inlr = *on;
// not passing date — procedure uses todays date.
dateds = getdate();
// passing date — for 1 january 2019.
MyDate = ‘20190101’;
dateds = getdate(MyDate);
// returns
[/cc]
returns for current date:
[cc lang=”php”] // returns
// EVAL dateds
// DATEDS.ERROR = ‘N’
// DATEDS.ISO = ‘2018-12-17′
// DATEDS.MDY = ’12/17/18′
// DATEDS.USA = ’12/17/2018’
// DATEDS.ISO0 = 20181217.
// DATEDS.MDY0 = 121718.
// DATEDS.USA0 = 12172018.
// DATEDS.JUL0 = 2458470.
// DATEDS.DAYNAME = ‘Monday ‘
// DATEDS.MONTHNAME = ‘December ‘
// DATEDS.LSTDAYCUR = ‘2018-12-31’
// DATEDS.LSTDAYCURISO = 20181231.
// DATEDS.LSTDAYCURMDY = 123118.
// DATEDS.LSTDAYCURCYMD = 1181231.
// DATEDS.FSTDAYCUR = ‘2018-12-01’
// DATEDS.FSTDAYCURISO = 20181201.
// DATEDS.FSTDAYCURMDY = 120118.
// DATEDS.FSTDAYCURCYMD = 1181201.
// DATEDS.LSTDAYLST = ‘2018-11-30’
// DATEDS.LSTDAYLSTISO = 20181130.
// DATEDS.LSTDAYLSTMDY = 113018.
// DATEDS.LSTDAYLSTCYMD = 1181130.
// DATEDS.FSTDAYLST = ‘2018-11-01’
// DATEDS.FSTDAYLSTISO = 20181101.
// DATEDS.FSTDAYLSTMDY = 110118.
// DATEDS.FSTDAYLSTCYMD = 1181101.
// DATEDS.STRING1 = ’17 Dec 2018 ‘
// DATEDS.STRING2 = ‘Monday, 17 Dec 2018 ‘
// DATEDS.STRING3 = ‘Monday, 17 December 2018 ‘
// DATEDS.TIME = ‘08.45.25’
// DATEDS.TIMESTAMP = ‘2018-12-17-08.45.25.252626’
// DATEDS.WORKISO = ‘2018-11-17’
// DATEDS.CYMD0 = 1181217.
//
[/cc]
Returns for January 1st 2019
[cc lang=”php”]
// EVAL dateds
// DATEDS.ERROR = ‘N’
// DATEDS.ISO = ‘2019-01-01′
// DATEDS.MDY = ’01/01/19′
// DATEDS.USA = ’01/01/2019’
// DATEDS.ISO0 = 20190101.
// DATEDS.MDY0 = 010119.
// DATEDS.USA0 = 01012019.
// DATEDS.JUL0 = 2458485.
// DATEDS.DAYNAME = ‘Tuesday ‘
// DATEDS.MONTHNAME = ‘January ‘
// DATEDS.LSTDAYCUR = ‘2019-01-31’
// DATEDS.LSTDAYCURISO = 20190131.
// DATEDS.LSTDAYCURMDY = 013119.
// DATEDS.LSTDAYCURCYMD = 1190131.
// DATEDS.FSTDAYCUR = ‘2019-01-01’
// DATEDS.FSTDAYCURISO = 20190101.
// DATEDS.FSTDAYCURMDY = 010119.
// DATEDS.FSTDAYCURCYMD = 1190101.
// DATEDS.LSTDAYLST = ‘2018-12-31’
// DATEDS.LSTDAYLSTISO = 20181231.
// DATEDS.LSTDAYLSTMDY = 123118.
// DATEDS.LSTDAYLSTCYMD = 1181231.
// DATEDS.FSTDAYLST = ‘2018-12-01’
// DATEDS.FSTDAYLSTISO = 20181201.
// DATEDS.FSTDAYLSTMDY = 120118.
// DATEDS.FSTDAYLSTCYMD = 1181201.
// DATEDS.STRING1 = ’01 Jan 2019 ‘
// DATEDS.STRING2 = ‘Tuesday, 01 Jan 2019 ‘
// DATEDS.STRING3 = ‘Tuesday, 01 January 2019 ‘
// DATEDS.TIME = ‘08.46.23’
// DATEDS.TIMESTAMP = ‘2018-12-17-08.46.23.423491’
// DATEDS.WORKISO = ‘2018-12-01’
// DATEDS.CYMD0 = 1190101.
//
[/cc]