ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Custom procedures with date type codes (e.g. *ISO0)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom procedures with date type codes (e.g. *ISO0)

    The date/time type codes (*ISO,*DMY,*ISO0, etc) you use with %CHAR to convert from a date/time, or with %DATE / %TIME / %TIMESTAMP to convert to a date/time.

    Are those simply keywords processed at compile time? Or is there a way to specify them as a field so you could write a custom procedure like this:

    Code:
    myProc(somedate:*ISO0);
    
    dcl-proc myProc
    
      dcl-pi myProc
        inDate char(10);
        inType ???
      end-pi
    
      somevar = %date(inDate:inType);
      some other processing;
    
    end-proc
    I don't need to do this, I'm just curious.

  • #2
    I quite like using CEEDAYS in conjunction with CEEDATE. You can then accept an input and output format as parameters and then format the date any which way you want. We had to do this for our Chinese company as they wanted a date format and the QLOCALE just didn't want to play with w.r.t. date formats.

    So you would do something like this:
    Code:
    Select;                                                                  
      // Incoming format *CYMD                                                
      when InFormatPR = 1;                                                    
           ISOdate = %date(InDatePR :*cymd);                                  
    ENDsl;                                                                    
    
    monitor;                                                                  
      CEEDAYS(%char(ISOdate :*iso) :CEEinFmtTxt :lilian  :*OMIT);            
      on-error;                                                              
        RtnIndPR = 'EE';                                                      
        OutDatePR = %char(ISOdate);                                          
    endmon;                                                                  
    
    if RtnIndPR <> 'EE';                                                      
       Select;                                                                
         // Outgoing format 'DD-Mmm-YYYY'                                    
         when OutFormatPR = 1;                                                
              CEEoutFmtTxt = 'DD-Mmm-YYYY';                                  
       ENDsl;                                                                
    
       monitor;                                                              
       CEEDATE(lilian :CEEoutFmtTxt :OutDatePR :*OMIT);                      
         on-error;                                                            
           RtnIndPR = 'EE';  
           OutDatePR =  %char(ISOdate);  
       endmon;                          
    ENDIF;
    You can change the '-' to whatever, the year to 2 char long. The Mmm would be Jan, and MMm would be JAn, etc. Very handy.
    Last edited by kitvb1; July 18, 2017, 08:17 AM.
    Regards

    Kit
    http://www.ecofitonline.com
    DeskfIT - ChangefIT - XrefIT
    ___________________________________
    There are only 3 kinds of people -
    Those that can count and those that can't.

    Comment


    • #3
      The format cannot be a variable it is fixed at compile time. Not sure why.

      Maybe one of these days when I have 5 I'll write up an RFE to request it be added - it would be useful.

      Comment


      • #4
        Originally posted by Vectorspace View Post
        The date/time type codes (*ISO,*DMY,*ISO0, etc) you use with %CHAR to convert from a date/time, or with %DATE / %TIME / %TIMESTAMP to convert to a date/time.

        Are those simply keywords processed at compile time? Or is there a way to specify them as a field so you could write a custom procedure like this:

        Code:
        myProc(somedate:*ISO0);
        
        dcl-proc myProc
        
        dcl-pi myProc
        inDate char(10);
        inType ???
        end-pi
        
        somevar = %date(inDate:inType);
        some other processing;
        
        end-proc
        I don't need to do this, I'm just curious.
        I'm not sure what you're questions is I guess - Define intype as char(5) - you pass the *ISO0 (or whatever format you want) and it will work.

        Comment


        • #5
          Originally posted by Rocky View Post

          I'm not sure what you're questions is I guess - Define intype as char(5) - you pass the *ISO0 (or whatever format you want) and it will work.
          But you can't have the second parameter of %date as an alphabetical field:

          inType char(5);
          inType = '*ISO0';
          %date(inDate:inType); // Compile error

          I was just wondering if there was a way to pass date type codes like *ISO0 in procedure parameters in a format that is compatible with use in %date etc. As JonBoy says, you can't, it's a keyword processed at compile time.

          Comment


          • #6
            Thought I had already answered this but ...

            All of the date and time format keywords - in ALL contexts - are handled at compile time.

            Comment


            • #7
              Originally posted by JonBoy View Post
              Thought I had already answered this but ...

              All of the date and time format keywords - in ALL contexts - are handled at compile time.
              Yes - but in the example given - it is valid - %DATE is a builtin function - you can pass a date to the program in character format and specify what format it's in... and then assign it to a date format - that isn't compile time, but run time. There may be some additional coding to deal with 6 digit dates (I"m not sure if %Date handles a *MDY0 format coming from a 10 character field - been awhile). But the point is - the concept is still there for run time...

              Comment


              • #8
                Sorry Rocky but I think you are wrong. If a date format is specified to %Date it must be known at compile time. In the latest releases that means that it could be a constant (although I haven't tested it) or a literal. It cannot be a variable.

                Comment


                • #9
                  Jonboy,

                  I hate to admit it... but you're right... for some reason I thought otherwise - probably because arguments to a function can be variables.... but it's still easy to solve:

                  Code:
                     dcl-pi dtetest date;                   
                       indate char(10) const;               
                       intyp  char(5) const;                
                     end-pi dtetest;                        
                     Dcl-s dtefld  date;                    
                     select;                                
                     when intyp = '*MDY0';                  
                         dtefld = %Date(indate: *mdy0);     
                     when intyp = '*USA0';                  
                         dtefld = %Date(indate: *usa0);     
                     when intyp = '*USA';                   
                         dtefld = %Date(indate: *usa);      
                     endsl;                                 
                     return dtefld;                         
                     end-proc    ;
                  It isn't formatted real nice - it was a quick proof of concept routine - and you'd have to add more when statements to cover the other possibilties...

                  Comment

                  Working...
                  X