IBMi (AS400) fans only : RPG Free Format: Example to get day of week

day of week




#IBMiSample


GETDAY.RPGLE

      **FREE
      // ***********************************************
      // *  RPG Free Format Sample to get day of week  *
      // *                                             *
      // *  Parameters:                                *
      // *  ppXDT0         6s 0 Date to decode ymd     *
      // *  ppXDAY        10A   day of week            *
      // *                                             *
      // *  How to call (for example):                 *
      // *  CALL PGM(ROUT58) PARM('190202' '   ')      *
      // ***********************************************
       ctl-opt DFTACTGRP(*NO);

       dcl-s tmpDate   char(10) Inz('2010-10-31');
       dcl-s tmpDate2  char(10) Inz('2010-10-31');
       dcl-s dayNbr    packed(11: 0) Inz(0);
       //    * PARAMETRI RICEVUTI (EX *ENTRY PLIST PARM)
       Dcl-pi GETDAY;
         ppXDT0 zoned(6);
             ppXDAY char(3);
       End-pi;
       tmpDate  = %char(%date(ppXDT0:*ymd):*iso0); // yymmdd to ccyymmdd
       tmpDate2 = %subst(tmpDate:1:4) + '-' + %subst(tmpDate:5:2) + '-'
       +%subst(tmpDate:7:2);
       // 0=Mon, 1=Tue, 2=Wed, 3=Thu, 4=Fri, 5=Sat, 6=Sun
       dayNbr = %rem(%diff(%date(tmpDate2):d'0001-01-01':*days):7);
       select;
       when dayNbr = 0;
         ppXDAY = 'Mon';
       when dayNbr = 1;
         ppXDAY = 'Tue';
       when dayNbr = 2;
         ppXDAY = 'Wed';
       when dayNbr = 3;
         ppXDAY = 'Thu';
       when dayNbr = 4;
         ppXDAY = 'Fri';
       when dayNbr = 5;
         ppXDAY = 'Sat';
       when dayNbr = 6;
         ppXDAY = 'Sun';
       endsl;
       *inlr = *on;







Thanks to Anonymous this second version using SQL dayname is better:


GETDAYSQL.sqlrpgle 

      **FREE                                               
      // ********************************************      
      // RPG Free Format Sample to get day of week  *      
      // Parameters:                                *      
      // ppXDT0         6s 0 Date ymd to decode     *      
      // ppXDAY         3A   day of week            *      
      // ********************************************      
      // CALL PGM(GETDAYSQL) PARM('210209' 'xxx')   *      
      // ********************************************      
       ctl-opt DFTACTGRP(*NO);                             
       dcl-s wkDate    date(*iso) ;                        
       // Received parms        
       Dcl-pi GETDAYSQL;                                   
         ppXDT0 zoned(6);                                  
         ppXDAY char(3);                                   
       End-pi;                                             
       wkDate = %date(ppXDT0: *ymd);                       
       exec sql set :ppXDAY = substr(dayname(:wkDate), 1, 3);
       dsply ppXDAY;                                          
              *inlr = *on;   

 

 I appreciate all the comments made on this blog.

 


Comments

  1. Well done! Thank you.

    ReplyDelete
    Replies
    1. PGM
      DCL VAR(&DAYOFWEEK) TYPE(*CHAR) LEN(4)
      RTVSYSVAL SYSVAL(QDAYOFWEEK) RTNVAR(&DAYOFWEEK)
      CHGVAR VAR(&DAYOFWEEK) VALUE(%SST(&DAYOFWEEK 2 3))
      SNDPGMMSG MSG("DOW: "&DAYOFWEEK)
      ENDPGM

      Delete
    2. Thank you for your beautiful example

      Delete
  2. //--Simpler with SQL:

    exec sql set :Nbr = dayofweek(:wkDate) ;

    exec sql set :Nbr = dayofweek_iso(:wkDate) ;

    The DAYOFWEEK and DAYOFWEEK_ISO functions are basically the same, they return an number that represents the day of the week.

    The difference is when the week starts. For DAYOFWEEK 1 is Sunday and 7 is Saturday. For DAYOFWEEK_ISO 1 is Monday and 7 is Sunday.

    ReplyDelete
  3. I would do this on this way:

    Create free compileTime array as ds.

    dcl-ds Week;
    *n char(3) inz('Mon');
    *n char(3) inz('Tue');
    *n char(3) inz('Wed');
    *n char(3) inz('Thu');
    *n char(3) inz('Fri');
    *n char(3) inz('Sat');
    *n char(3) inz('Sun');
    Weekday char(3) dim(7) pos(1);
    end-ds;

    dsply Weekday(dayNbr);

    I don't like CTA's at the end of the programs

    ReplyDelete
  4. If your goal is to get the name of the day, you can do it in one line using SQL:

    exec sql set :day = dayname(:wkDate);

    For example, to get the name of today:

    exec sql set :day = dayname(Current_Date);

    ReplyDelete
  5. If you want just the first three characters of the day name as your program does, you can do it in one line using SQL:

    exec sql set :day = substr(dayname(:wkDate),1,3);

    ReplyDelete
  6. thanks again.
    I updated the example with
    exec sql set :ppXDAY = substr(dayname(:wkDate),1,3);

    ReplyDelete

Post a Comment

Popular posts from this blog

(IBM i fans only) Efficient WRKSPLF with WSF - How to Search string into spooled files, Sort, and Generate PDFs on IBMi

(IBM i fans only) Detecting and Handling Non-Printable Characters in DB2 SQL Using LOCATE() and REPLACE() Functions

(IBM i fans only) How to Sniff User Access