ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Calculate Yesterday's Date? (CLP or RPG/400)

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

  • Calculate Yesterday's Date? (CLP or RPG/400)

    Hi

    Can some please advise me how to calculate yesterday's date in either a CLP or RPG/400?

    Ideally this would be to retrieve the System Date, then subtract a day from it and have an end format of DDMMYY.

    Thanks
    Simon

  • #2
    Re: Calculate Yesterday's Date? (CLP or RPG/400)

    Code:
    [SIZE=2][COLOR=#ff0000][SIZE=2][COLOR=#0000aa]d Yesterday       s               d   DatFmt( *mdy )      
                                                              
     /free                                                    
                                                              
       Yesterday = %date - %days( 1 );  
                          
       dsply %char( Yesterday );                             
                                                              
       *inlr = *on;                                          [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000aa][SIZE=2][COLOR=#0000aa]
    [/COLOR][/SIZE][/COLOR][/SIZE]

    Michael Catalani
    IS Director, eCommerce & Web Development
    Acceptance Insurance Corporation
    www.AcceptanceInsurance.com
    www.ProvatoSys.com

    Comment


    • #3
      Re: Calculate Yesterday's Date? (CLP or RPG/400)

      Thanks - and that is valid RPG/400 is it?

      Comment


      • #4
        Re: Calculate Yesterday's Date? (CLP or RPG/400)

        Simon,

        Michael's code was RPGIV (free form).


        Rico

        Comment


        • #5
          Re: Calculate Yesterday's Date? (CLP or RPG/400)

          If its truly RPG/400, then it is likely RPGIII. (Technically, a program is either RPGIII or RPGIV on this platform. )

          If the program you are working on is RPGIII, then use CVTRPGSRC first to convert it to the RPGIV layout, add the one line of date code, and be done.

          If your boss doesnt allow you to convert an RPGIII program to RPGIV, then have him write the the date routine to subtract a day, or a month, or 3 years, or add 6 hours too the current time, et.c in under ten seconds in RPGIII, which is the amount of time needed to run both the CVTRPGSRC command and add the one line of code to pull this off. In all seriousness, if your company is doing anything with date durations or converting to and from different date formats, you have to be coding in RPGIV. (Which is now about 18 years old. RPGV will be out soon.)
          Last edited by MichaelCatalani; December 13, 2011, 08:38 PM.
          Michael Catalani
          IS Director, eCommerce & Web Development
          Acceptance Insurance Corporation
          www.AcceptanceInsurance.com
          www.ProvatoSys.com

          Comment


          • #6
            Re: Calculate Yesterday's Date? (CLP or RPG/400)

            If your boss is still not happy with that, get him to call Michael ....
            Greg Craill: "Life's hard - Get a helmet !!"

            Comment


            • #7
              Re: Calculate Yesterday's Date? (CLP or RPG/400)

              Works fine - I wanted a stub pgm to call from a CLP. There's no problem with running RPGLE, it's just our legacy system is RPGIII and no-one knows RPGLE; we're mainly VB, .NET & C# developers now!

              Thanks for help.

              Comment


              • #8
                Re: Calculate Yesterday's Date? (CLP or RPG/400)

                if you wanted to do this in CL....

                cvtdat -- to convert to julian. add 1 to it and convert back to the format you want.
                But I hate CL!
                Tommy did this 45 years ago on another forum

                Code:
                pgm                                                                    
                   dcl &date *char 6                                                   
                   dcl &juldate *char 5                                                
                   dcl &numday *dec (3 0)                                              
                   dcl &outdate *char 10                                               
                                                                                      
                             RTVSYSVAL  SYSVAL(QDATE) RTNVAR(&DATE)                    
                             CVTDAT     DATE(&DATE) TOVAR(&JULDATE) FROMFMT(*SYSVAL) + 
                                          TOFMT(*JUL) TOSEP(*NONE)                     
                             chgvar &numday %SST(&juldate 3 3)                         
                             chgvar &numday (&numday + 1)                              
                             chgvar %sst(&juldate 3 3) &numday                         
                             CVTDAT     DATE(&JULDATE) TOVAR(&OUTDATE) FROMFMT(*JUL) + 
                                          TOFMT(*SYSVAL)                               
                                          return                                       
                                          endpgm
                The "recommended" way to do it in CLLE!
                Code:
                [COLOR=black]/*  This is ILE CL.   The source member type needs to be CLLE.  If you[/COLOR]
                [COLOR=black]    compile from the command-line, use CRTBNDCL (never CRTCLPGM!)  */[/COLOR]
                [COLOR=black]PGM[/COLOR]
                [COLOR=black]    DCL VAR(&DATE)     TYPE(*CHAR) LEN(6)[/COLOR]
                [COLOR=black]    DCL VAR(&LILIAN)   TYPE(*CHAR) LEN(4)[/COLOR]
                [COLOR=black]    DCL VAR(&FILENAME) TYPE(*CHAR) LEN(10)[/COLOR]
                
                [COLOR=black]    CHGVAR VAR(&DATE) VALUE('011206')[/COLOR]
                
                [COLOR=black]    /* The CEEDAYS API converts a date to lilian (which is a count +[/COLOR]
                [COLOR=black]          of the number of days since 14 October 1582).            +[/COLOR]
                [COLOR=black]       You can add or subtract numbers from the Lilian date to     +[/COLOR]
                [COLOR=black]          add or subtract days (e.g. subtract 1)                   +[/COLOR]
                [COLOR=black]       The CEEDATE API converts the lilian back to an ordinary     +[/COLOR]
                [COLOR=black]          date.                                                   */[/COLOR]
                
                [COLOR=black]    CALLPRC PRC(CEEDAYS) PARM(&DATE 'DDMMYY' &LILIAN *OMIT)[/COLOR]
                [COLOR=black]    CHGVAR VAR(%BIN(&LILIAN)) VALUE(%BIN(&LILIAN) - 1)[/COLOR]
                [COLOR=black]    CALLPRC PRC(CEEDATE) PARM(&LILIAN 'DDMMYY' &DATE *OMIT)[/COLOR]
                
                [COLOR=black]    /*  Add .txt to create a filename, then display it on the screen +[/COLOR]
                [COLOR=black]        for the sake of demonstration.  (In a real program, you'd    +[/COLOR]
                [COLOR=black]        use the filename as the output of CPYTOIMPF or someother IFS +[/COLOR]
                [COLOR=black]        command rather than just displaying it.)                    */[/COLOR]
                
                [COLOR=black]    CHGVAR VAR(&FILENAME) VALUE(&DATE *CAT '.txt')[/COLOR]
                
                [COLOR=black]    SNDUSRMSG MSG(&FILENAME) MSGTYPE(*INFO)[/COLOR]
                
                [COLOR=black]ENDPGM[/COLOR]
                also found this
                Code:
                            PGM                                                                         
                            DCL        VAR(&DATE) TYPE(*CHAR) LEN(6) VALUE('010107')                    
                            DCL        VAR(&OUTDATE) TYPE(*CHAR) LEN(6)                                 
                            DCL        VAR(&JULIAN) TYPE(*CHAR) LEN(5)                                  
                            DCL        VAR(&DECJULIAN) TYPE(*DEC) LEN(5)                                
                            DCL        VAR(&YEAR) TYPE(*CHAR) LEN(2)                                    
                            DCL        VAR(&DECYEAR) TYPE(*DEC) LEN(2)                                  
                            DCL        VAR(&DAYS) TYPE(*CHAR) LEN(3)                                    
                            DCL        VAR(&DAYS) TYPE(*CHAR) LEN(3)                                    
                            DCL        VAR(&DECDAYS) TYPE(*DEC) LEN(3)                                  
                                                                                                        
                      /*    RTVSYSVAL  SYSVAL(QDATE) RTNVAR(&DATE) */                                   
                            CVTDAT     DATE(&DATE) TOVAR(&JULIAN) FROMFMT(*MDY) +                       
                                         TOFMT(*JUL) TOSEP(*NONE)                                       
                                                                                                        
                            CHGVAR     VAR(&YEAR) VALUE(%SST(&JULIAN 1 2))                              
                            CHGVAR     VAR(&DECYEAR) VALUE(&YEAR)                                       
                            CHGVAR     VAR(&DAYS) VALUE(%SST(&JULIAN 3 3))                              
                            CHGVAR     VAR(&DECDAYS) VALUE(&DAYS)                                       
                            IF         COND(&DECDAYS <= 7) THEN(DO)                                
                            CHGVAR     VAR(&DECYEAR) VALUE(&DECYEAR - 1)                           
                            CHGVAR     VAR(&DECDAYS) VALUE(365 - (7 - &DECDAYS))                   
                            chgvar &YEAR &DECYEAR                                                  
                            chgvar &DAYS &DECDAYS                                                  
                            CHGVAR     VAR(&JULIAN) VALUE(&YEAR || &DAYS)                          
                            CVTDAT     DATE(&JULIAN) TOVAR(&OUTDATE) FROMFMT(*JUL) +               
                                         TOFMT(*YMD) TOSEP(*NONE)                                  
                            ENDDO                                                                  
                            ELSE  CMD(DO)                                                          
                            CHGVAR &DECJULIAN &JULIAN                                              
                            CHGVAR     VAR(&DECJULIAN) VALUE(&DECJULIAN + 1)                       
                            CHGVAR &JULIAN  &DECJULIAN                                             
                            CVTDAT     DATE(&JULIAN) TOVAR(&OUTDATE) FROMFMT(*JUL) +               
                                         TOFMT(*YMD) TOSEP(*NONE)                                  
                            ENDDO                                                                  
                                                                                                   
                            ENDPGM
                jamie
                All my answers were extracted from the "Big Dummy's Guide to the As400"
                and I take no responsibility for any of them.

                www.code400.com

                Comment


                • #9
                  Re: Calculate Yesterday's Date? (CLP or RPG/400)

                  Originally posted by jamief View Post
                  Tommy did this 45 years ago on another forum
                  @#%@#%@#%@#% I'M GOOD!!! I coded that from the womb!!!!
                  I'm not anti-social, I just don't like people -Tommy Holden

                  Comment


                  • #10
                    Re: Calculate Yesterday's Date? (CLP or RPG/400)

                    One caveat regarding the System Date and the final output as DDMMYY format: when converting to Lillian, you need to make sure the parm for the "picture string" second parm is the same as the format for the system date (QDATE). The example below (which plagiarizes most of Jamie's code) is based on a box that has a system date in MMDDYY format. It also recycles the same field, but format is MMDDYY when converting to Lillian but it will be DDMMYY for the final result.

                    Code:
                       DCL   &DATE   *CHAR   6
                    [COLOR=#000000]  DCL   &LILLIAN *CHAR  4[/COLOR]
                    
                    
                       RTVSYSVAL  QDATE(&DATE)
                      
                    [COLOR=#000000]  CALLPRC PRC(CEEDAYS) PARM(&DATE 'MMDDYY' &LILIAN *OMIT)[/COLOR]
                    [COLOR=black]  CHGVAR VAR(%BIN(&LILLIAN)) VALUE(%BIN(&LILLIAN) - 1)[/COLOR]
                    [COLOR=black]  CALLPRC PRC(CEEDATE) PARM(&LILLIAN 'DDMMYY' &DATE *OMIT)[/COLOR]
                    Hopefully that helps. I've re-re-read this post to make sure it sounds logical. However, something tells me that adding to what has already been said is going to gum up the works and make things more confusing.
                    http://www.linkedin.com/in/chippermiller

                    Comment

                    Working...
                    X