Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
beginMonth = myDate - %Days(%subdt( myDate:*days)-1);
endMonth = (myDate + %Months(1)) - %days(%subdt(myDate + %Months(1):*days));
.....H bnddir('COZTOOLS/COZTOOLS') H DFTACTGRP(*NO) ACTGRP(*NEW) D/include cozTools/qcpysrc,joblog D myDate S D Inz(*SYS) D BOM S D D EOM S D C MOVE *ON *INLR /free BOM = myDate - %Days(%subdt( myDate:*days)-1); EOM = (myDate+%Months(1)) - %days(%subdt(myDate + %Months(1):*days)); joblog('myDate: %s BOM: %s EOM: %s': %char(myDate:*USA): %char(BOM:*USA): %char(EOM:*USA)); /end-free
Performance question: Is the compiler smart enough to recognize that the repeated subexpression in endMonth has no side effects, and therefore can be reused, or would it be better to explicitly do the intermediate calc with a work field?
wrkDat = myDate + %Months(1);
endMonth = wrkDat - %days(%subdt(wrkDat:*days));
I would personally do the extra step myself because then its going to use the variable no matter what. RPG IV's optimizer does however optimize simple redundancies like this specific one. So I think it wouldn't really matter.
endMonth = BeginMonth + %Months(1) - %Days(1) ;
Chris Ringer
Chris, I actually have/had that in my original example, however it doesn't stand-alone for a given date. You'd have to first calculate the 1st of the month, then pass that date to the EOM routine. So I made it more stand-alone.
Well hey, I know you'd make those sub-procedures so... Cozzi_BeginMonth() would be available anytime.
As would getEndOfMonth()