Midrange News for the IBM i Community


Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
Chicagoland
Get 1st of Month and End of Month in RPGIV
has no ratings.
Published: 19 Sep 2012
Revised: 23 Jan 2013 - 4110 days ago
Last viewed on: 24 Apr 2024 (8738 views) 

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.

Get 1st of Month and End of Month in RPGIV Published by: Bob Cozzi on 19 Sep 2012 view comments(6)

Calculating the 1st of Month in RPG IV

beginMonth = myDate - %Days(%subdt( myDate:*days)-1);  

Calculating the End of Month in RPG IV

endMonth = (myDate + %Months(1)) - %days(%subdt(myDate + %Months(1):*days));

 Test Program that Shows Results

.....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                                              

 

 

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: DaleB
Premium member *
Reading, PA
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 11 years 7 months 3 days 16 hours 13 minutes ago

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));

 

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 11 years 7 months 3 days 13 hours 51 minutes ago

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.

Posted by: Ringer
Premium member *
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 11 years 7 months 18 hours 56 minutes ago

endMonth = BeginMonth + %Months(1)  - %Days(1) ;

Chris Ringer

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 11 years 7 months 15 hours 30 minutes ago

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.

Posted by: Ringer
Premium member *
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 11 years 7 months 13 hours 19 minutes ago

Well hey, I know you'd make those sub-procedures so... Cozzi_BeginMonth() would be available anytime. Laughing

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 11 years 7 months 11 hours 3 minutes ago

As would getEndOfMonth()