tnt400.com - AS/400 Tips And Techniques

Sponsored by news400.com

This page is a discussion on the mentioned topic.
Most of the answers are in their original posted form, including any technical/spelling/grammatical errors.
No guarantees are expressed or implied. :-)
Comments, corrections, concerns about this tip?

Got another AS/400 question? Ask it here


What's New?
See what's new at Tips-N-Tech.

AS/400 Tips-N-Tech
AS/400 tips, techniques, and FAQ. Updated frequently.
CODEPage/400
All the code samples you can eat! RPG, CL, DDS, etc. etc.
AS/400 FAQs
The official news400.com FAQs.






All AS/400 Tip Categories / AS/400 Programming / Get day of the week in RPG


Question:

Hi

Is there an easy way to get the day of the week in ILERPG, or do I need to use the CEEDAYS/CEEDYWK APIs. If so an example would be appreciated.

Thanks


Books on this subject:
RPG IV Jump Start : Moving Ahead With the New RPG
AS/400 Expert : Ready-To-Run RPG/400 Techniques
RPG IV By Example
Answer(s):



Here is the day of week module which I lifted from Bryan Meyer's book RPG IV Jump Start.
     H NoMain
     H DatFmt( *ISO )

     D GetDow          PR             1P 0
     D  InpDate                        D   Value

     P GetDow          B                   Export

     D                 PI             1P 0
     D  InpDate                        D   Value

     D DayOfWk         S             11P 0

     C     InpDate       SubDur    D'1998-08-01' DayOfWk:*D
     C                   Div       7             DayOfWk
     C                   MvR                     DayOfWk

     C                   If        DayOfWk > 0
     C                   Return    DayOfWk
     C                   Else
     C                   Return    DayOfWk + 7
     C                   EndIf

     P GetDow          E
This procedure returns 1=Sunday, 2=Monday, etc. Here is a quick module I wrote to test this module.
     D GetDow          PR             1P 0
     D  InpDate                        D   Value

     D PrmDate         S               D   DatFmt( *MDY )
     D Dow             S              1P 0

     C     *Entry        Plist
     C                   Parm                    PrmDate

     C                   Eval      Dow = GetDow( PrmDate )
     C     Dow           Dsply
     C                   Eval      *INLR = *On

Compile these two modules with PDM option 15. Then bind them together with the CrtPgm command. From the command line you invoke by call the program...
Call WhatEverYouNamedTheProgram 'DateInMM/DD/YY-Format'
Mike Cravitz
NEWS/400 Technical Editor




Here's some water to jump in: I swear that I copied this originally out of a RPG manual, but I cannot remember which version/release. I wasn't able to find it in V4R2. There is another example for functions, CvtToHex, and I have no access to the other manuals at this time.

H DEBUG
 *
D DayOfWeek       PR             1P 0
D  DateIn                         D
 *
D Week_Date       S               D   INZ
D WeekDay         S              1P 0
 *
D Date            S              6S 0 INZ(120997)
 *
C     *DMY          MOVE      Date          Week_Date
C                   EVAL      WeekDay = DayOfWeek(Week_Date)
 * 1=MON,2=TUE,3=WED,4=THU,5=FRI,6=SAT,7=SUN
C                   DUMP
C                   MOVE      *ON           *INLR

P DayOfWeek       B
D                 PI             1P 0
D   DateIn                        D
 *
D Weeks           S              7  0
D DayNbr          S              1P 0
D AnySunday       S               D   INZ(d'1997-04-27')
 *
C     DateIn        SUBDUR    AnySunday     Weeks:*D
C                   DIV       7             Weeks
C                   MVR                     DayNbr
C                   IF        DayNbr <1 C EVAL DayNbr="DayNbr" + 7 C ENDIF C RETURN DayNbr P DayOfWeek E 
Hope this helps.




Just jump in the water and copy the function DayofWeek in the ILE RPG manual to a SRC-PF.

You then just code EVAL Weekday = DayofWeek(Datefield)




This piece of code tells you the day of the week today is that I made for another purpose, but you can modify it easily enough.

      *********************************************************************
      * Compile-time array: AL  = days to first of a month, not leap year:
      * Compile-time array: AR  = days to first of a month, leap year:
      * Compile-time array: ARD = DAYS of the week(The field we want).
     DAL               S              3S 0 DIM(12) PERRCD(12) CTDATA
     DAR               S              3S 0 DIM(12) PERRCD(12) CTDATA
     DARD              S              9A   DIM(7) PERRCD(7) CTDATA
      * .................................................................
      * Step 1: find the # of days through the end of last year:
     C     *YEAR         SUB       1             LASTY             4 0
     C     LASTY         MULT      365.25        DAYS              7 0
      * .............
      *  Step 2: add the days so far this year:
      *    Is it Leap year? (TESTLY if = 0, then its leap year.)
     C     *YEAR         MULT      25            TESTLY            2 0
      *    Days so far this year, to the first of this month:
     C                   Z-ADD     UMONTH        I                 2 0
     C     TESTLY        IFNE      *ZEROS
     C                   ADD       AR(I)         DAYS
     C                   ELSE
     C                   ADD       AL(I)         DAYS
     C                   END
      *     Then, add in the days so far this month:
     C                   ADD       UDAY          DAYS
      * .............
      * Step 3: Now we have the total number of days since 01/01/0001.
      *         By removing all full weeks since 01/01/0001 we will have
      *         established today's "offset" from 01/01/0001. That
      *        "offset" is the remainder, and is useful as an array index.
     C     DAYS          DIV       7             DAYS
     C                   MVR                     I
      * .............
      * Step 4: Logically the remainder can only be 0 - 6.
      *         But 0 is a lousy index so by adding 1 we assure
      *         that the index will be 1-7, and therefore useful.
     C                   ADD       1             I
      * .............
      * Step 5: Now, with the index, look up the day of the week.
     C                   MOVE      ARD(I)        TODAY             9
      * A frequently asked question is "Hey, how did you know what day
      * to start with in the ARD array?"  Simple.  The first time I
      * did this I started it with Sunday.  It was off by one day so I
      * started the array with Monday instead; then I was off by two
      * days.  But Saturday worked fine.
      * .................................................................
      *
** FebMarAprMayJunJulAugSepOctNovDec   (AR days, not leap year)
000031059090120151181212243273304334
**                                     (AL days, leap year.
000031060091121152182213244274305335
** DAYS of the week: (becomes "TODAY") (ARD Day-of-week)
Saturday Sunday   Monday   Tuesday  WednesdayThursday Friday






Well as a last resort, if you are calling it from a controlling CL program, use RTVSYSVAL to just pass the system value QDAYOFWEEK to your program. It's 4 byte char. values *MON *TUE *WED....etc.


You are at a news400.com site.
Contact Us | Report Bugs | Submit Comments/Suggestions | Read Site Use Agreement | Read Privacy Policy
Copyright © 2000 Duke Communications International.
This site is best viewed with the latest versions of Netscape or Internet Explorer, 800 x 600 resolution (or higher), and at least 256 colors.
Duke Communications   NEWS/400 | 29th Street Press | Business Finance | DominoPro | Selling AS/400 Solutions | SQL Server Magazine | Windows NT Magazine