We received this tip regarding data calculations on the AS/400 that Kevin Ketzler thought would be helpful to System i users. If you have a tip, please send it to our editor's inbox.
As a consultant, over the course of many projects at different places, I periodically would have to calculate certain dates relative to a given date to determine reporting limits (e.g., month end, month begin). I also often had to determine the day of the week. So, I put together a little text file that first contained the calculations for these values. I have since converted the text file to contain procedures so that I can easily create a service program to perform the various date calculations.
h nomain option(*nodebugio:*srcstmt)
d getPriorMonthEnd...
d pr d
d pDate d const
d getPriorMonthBegin...
d pr d
d pDate d const
d getMonthBegin pr d
d pDate d const
d getMonthEnd pr d
d pDate d const
d getDayOfWeek pr 1p 0 Mon = 0 ... Sun = 6
d pDate d const
d isWeekDay pr n
d pDate d const
d isWeekEnd pr n
d pDate d const
p getPriorMonthEnd...
p b export
d getPriorMonthEnd...
d pi d
d pDate d const
/free
return pDate - %days(%subdt(pDate:*days));
/end-free
p getPriorMonthEnd...
p e
p getPriorMonthBegin...
p b export
d getPriorMonthBegin...
d pi d
d pDate d const
/free
return getMonthBegin(pDate - %months(1));
/end-free
p getPriorMonthBegin...
p e
p getMonthBegin b export
d getMonthBegin pi d
d pDate d const
/free
return getPriorMonthEnd(pDate) + %days(1);
/end-free
p getMonthBegin e
p getMonthEnd b export
d getMonthEnd pi d
d pDate d const
/free
return getPriorMonthEnd(pDate + %months(1));
/end-free
p getMonthEnd e
p getDayOfWeek b export
d getDayOfWeek pi 1p 0 Mon = 0 ... Sun = 6
d pDate d const
/free
return %rem(%diff(pDate:d'0001-01-01':*days):7);
/end-free
p getDayOfWeek e
p isWeekDay b export
d isWeekDay pi n
d pDate d const
/free
return getDayOfWeek(pDate) < 5;
/end-free
p isWeekDay e
p isWeekEnd b export
d isWeekEnd pi n
d pDate d const
/free
return getDayOfWeek(pDate) > 4;
/end-free
p isWeekEnd e
ABOUT THE AUTHOR: Kevin C. Ketzler is a designer, analyst, and project lead for IBM Solutions Practice at Affiliated.