Sunday, July 3, 2011

Converting a Date (Even If Invalid) To The Proper Format in RPG IV

So you are dealing with an old file and in that file there is a date field. All cool but then you realize that this file is getting to you via a flat file and the customer that is sending it has no idea how to create a valid date and most of the times the dates are correct but sometimes they are not.  They could be blank or contain the month, day or year in the wrong place.  

The thing is your company wants a display program to allow the correction of the data the customers are sending.  So you have a problem.  You cannnot use a proper date field because the date in the file could be invalid.  You cannot use the %DATE or the %DEC to play with the date because they won't work. So what do you do?

Well I had this exact problem. I had to display these fields on the screen and then allow correction into a proper date. So I created these functions.  I added them to a service program but they can be used as internal functions in OPM mode (Non-ILE) as long as you use RPG IV:

Here they are:

      // +----------------------------------------------------------------+
      // + Procedure CvtDteToMDYY. Convert Date from YYYYMMDD to MMDDYYYY +
      // +     Note: Must Do it This way because date could be invalid    +
      // + Author.:  Daniel Batista                                       +
      // + Version:  1.0                                                  +
      // + Date...:  12/14/2006                                           +
      // +                                                                +
      // + Modification Log:                                              +
      // +    12/14/2006.  Daniel Batista (DB Consulting)                 +
      // +       - Created Original Version                               +
      // +----------------------------------------------------------------+
     P CvtDteToMDYY    B                                                  
     D CvtDteToMDYY    PI             8S 0                                
     D  piDteInYYMD                   8S 0 Const                          
                                                                          
      * ----->  Locally Defined Variables                                 
     D laWkInDte       S              8A                                  
     D laWkOutDte      S              8A                                  
                                                                          
      /Free                                                               
         // Convert Date from YYYYMMDD to MMDDYYYY                        
         laWkInDte = %EditC(piDteInYYMD: 'X');    // Convert Date to String
         laWkOutDte = %SubSt(laWkInDte: 5: 2) + %SubSt(laWkInDte: 7: 2) + 
                      %SubSt(laWkInDte: 1: 4);                            
                                                                          
         Return %Dec(laWkOutDte: 8: 0);                                   
      /End-Free                                                           
                                                                          
     P CvtDteToMDYY    E                                                  
                                                                         
      // +----------------------------------------------------------------+
      // + Procedure CvtDteToYYMD. Convert Date from MMDDYYYY to YYYYMMDD +
      // +     Note: Must Do it This way because date could be invalid    +
      // +                                                                +
      // + Author.:  Daniel Batista                                       +
      // + Version:  1.0                                                  +
      // + Date...:  12/14/2006                                           +
      // +                                                                +
      // + Modification Log:                                              +
      // +    12/14/2006.  Daniel Batista (DB Consulting)                 +
      // +       - Created Original Version                               +
      // +                                                                +
      // +----------------------------------------------------------------+
     P CvtDteToYYMD    B                                                  
     D CvtDteToYYMD    PI             8S 0                                
     D  piDteInMDYY                   8S 0 Const                          
                                                                          
      * ----->  Locally Defined Variables                                 
     D laWkInDte       S              8A                                  
     D laWkOutDte      S              8A                                  
                                                                          
      /Free                                                               
         // Convert Date from MMDDYYYY to YYYYMMDD                        
         laWkInDte = %EditC(piDteInMDYY: 'X');    // Convert Date to String
         laWkOutDte = %SubSt(laWkInDte: 5: 4) + %SubSt(laWkInDte: 1: 2) + 
                      %SubSt(laWkInDte: 3: 2);                            
                                                                          
         Return %Dec(laWkOutDte: 8: 0);                                   
      /End-Free                                                           
                                                                          
     P CvtDteToYYMD    E
                                                                          


Here are the prototypes for the functions:
     D CvtDteToMDYY    PR             8S 0                      
     D  DteInYYMD                     8S 0 Const                
                                                                
     D CvtDteToYYMD    PR             8S 0                      
     D  DteInMDYY                     8S 0 Const
     


This example converts a date field into YYYYMMDD format:
      /Free
         If ScInvDte <> *Zero;                                   
            HInvDt = CvtDteToYYMD(ScInvDte);                     
         EndIf;
  
      /End-Free
  

1 comment:

  1. merkur 3 merkur - Review by Dmitry Merkur | Мисполька
    Merkur 3 merkur is a 5-piece double edge safety razor with worrione a polished chrome 1xbet korean finish and a 메리트 카지노 unique head design.

    ReplyDelete

DirectSwift Directory URL Shack Web Directory