Q
Problem solve Get help with specific problems with your technologies, process and projects.

Retrieving the system date in a CL program

I have retrieved the system date (QDATE) using RTVSYSVAL in my CL program. I know to advance the date by one date, but how can I achieve this within the CL program? I do not want to make an external call to a program.

You would have to use the convert date to convert your date to Julian, add 1 to it, check for leap year and make adjustments and then convert it back. A good example of this technique used to be found in the old TAATools in the ADDDAT command. Here is that example. This command has a six-character date but that can easily be changed with a very slight modification.

/* Add date command source ** 

CMD        PROMPT('Add Date')                          
PARM       KWD(DAYS) TYPE(*DEC) LEN(5) RANGE(-35000 +  
             35000) MIN(1) PROMPT('Nbr of days to +    
             add/sub   (5 0)')                         
PARM       KWD(TOVAR) TYPE(*CHAR) LEN(6) RTNVAL(*YES) +
             MIN(1) PROMPT('New date variable          
             (6)')                                     
PARM       KWD(DATE) TYPE(*DEC) LEN(6 0) DFT(*TODAY) + 
             RANGE(000000 999999) SPCVAL((*TODAY 0)) + 
             PROMPT('Date (sys fmt)           (6 0)')  

 

/* Add date CL source ** 

PGM        PARM(&DAYS &TOVAR &DATE)                        
DCL        &DAYS *DEC LEN(5 0)                             
DCL        &TOVAR *CHAR LEN(6)                             
DCL        &DATE *DEC LEN(6 0)                             
DCL        &WRKDAT *CHAR LEN(6)                            
DCL        &JULIANA *CHAR LEN(5)                           
DCL        &YRD *DEC LEN(2 0)                              
DCL        &DAYSD *DEC LEN(3 0)                            
DCL        &LEAP *DEC LEN(2 0)                             
DCL        &DAYSINYEAR *DEC LEN(3 0)                       
DCL        &NUM5 *DEC LEN(5)                               
DCL        &NUM2 *DEC LEN(2)                               
           /* &DATE=0 is special value *TODAY */           
IF         (&DATE *EQ 0) RTVJOBA DATE(&WRKDAT)             
IF         (&DATE *NE 0) CHGVAR &WRKDAT &DATE              
CVTDAT     DATE(&WRKDAT) TOVAR(&JULIANA) TOFMT(*JUL) +     
             TOSEP(*NONE) /* Convert to Julian */          
MONMSG     MSGID(CPF0555) EXEC(SNDPGMMSG +                 
           MSGID(CPF9898) MSGF(QCPFMSG) MSGTYPE(*ESCAPE) + 
           MSGDTA('DATE parameter value cannot +           
           be converted'))                                 
           /* Substring for year and day */                
CHGVAR     &YRD %SST(&JULIANA 1 2)                         
CHGVAR     &DAYSD %SST(&JULIANA 3 3)  

             CHGVAR     VAR(&NUM5) VALUE(&DAYSD + &DAYS) /* Add days */  
 CHKPLUS:    IF         (&NUM5 *GT 0) GOTO CHKLEAP /* If positive */     
             IF         (&YRD *EQ 00) CHGVAR &YRD 99 /* Year 2000 */     
             ELSE       CHGVAR &YRD (&YRD -1) /* Decrement year */       
 CHKLEAP:    CHGVAR     &NUM2 (&YRD / 4) /* Chk leap year */             
             CHGVAR     &LEAP (&YRD - (&NUM2 * 4))                       
             IF         (&LEAP *GT 0) CHGVAR &DAYSINYEAR 365             
             ELSE       CHGVAR &DAYSINYEAR 366 /* Leap year */           
             IF         (&NUM5 *LE 0) DO /* Days are negative */         
             CHGVAR     &NUM5 (&NUM5 + &DAYSINYEAR)                      
             GOTO       CHKPLUS /* Check for positive days */            
             ENDDO      /* End negative days */                          
             IF         (&NUM5 *GT &DAYSINYEAR) DO /* Ovfl */            
             IF         (&YRD *EQ 99) CHGVAR &YRD -1 /* Year 2000 */     
             CHGVAR     &YRD (&YRD + 1)  /* Bump year */                 
             CHGVAR     &NUM5 (&NUM5 - &DAYSINYEAR) /* Subtract */       
             GOTO       CHKLEAP /* Test for next year */                 
             ENDDO      /* End days greater than days-in-year */         
             CHGVAR     &DAYSD &NUM5  /* Chg to 3 digits */              
                             /* Substring back into Julian date */       
             CHGVAR     %SST(&JULIANA 1 2) &YRD                          
             CH 

Dig Deeper on RPG iSeries programming

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataCenter

Close