It's Summer, But Do You Know What Day of the Week It Is?

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

In the northern hemisphere, it's summer, while down under, winter has moved in. Here in Chicagoland, we're up in the high 90s with little to no rain. Not good for the most fertile farmland area in the world, but hey, most of it is covered with pavement nowadays anyway...but I digress.

With summer upon us and the holiday/vacation schedule in full swing, many people are putting off projects until after the baseball or golf season ends (although there are some people who believe the golf season never ends).

This week, I thought I'd give you a little project to work on over the summer. It's called Zeller's congruence. Have you heard of it? Go to google.com and enter "Zeller's congruence" and see what turns up.

It's always amazed me that RPG IV does math formulas so much more easily than RPG III did, yet it's rare that we see anything fun written in the language. Back in the 1980s, there was an RPG II program called something like "Star Trek" that had the whole storyline in it--Klingons, the Enterprise, sectors to defend, etc. It was hugely popular on the System/34. But then it sort of disappeared.

Zeller's congruence isn't a game; it's a very efficient way to calculate the day of the week, given any date. While the OS/400 CEEDYWK API will do this for you, sometimes it's fun to play with RPG.

OK, you didn't think I was actually going to ask you write an RPG IV procedure that implements Zeller's congruence, did you? After all, it is summer time. So I did it for you.

Listed below is the RPG IV subprocedure named GetDayEx(). This subprocedure accepts a date value in any supported RPG IV date data-type format, and it will calculate the day of the week. Zeller's congruence returns a zero-based day identifier, with Monday being day zero. So I've modified the result to a ones-based return value, with Sunday being equal to one. If you want pure Zeller, you can remove that last group of Calc specs before the RETURN opcode that do the adjustment.

Here's the code for Zeller's congruence, implemented in RPG IV as a standalone subprocedure.

      ** --------------------------------------------------------------
      **  G E T D A Y E X  -  Get Day using the Zeller's congruence
      ** --------------------------------------------------------------
     **      (c) Copyright 2002 by Robert Cozzi, Jr.
     **          All rights reserved.
     **          Part of the RPG xTools. Used by Permission.
     **          www.rpgxtools.com
     P GetDayEx        B                   Export
     D GetDayEx        PI            10I 0
     D  InDate                         D   Const DATFMT(*ISO)

     D nPart1          S             10I 0
     D nDay            S             10I 0
     D nMonth          S             10I 0
     D nYear           S             10I 0
     D nMod            S             10I 0
     D nDayOfWeek      S             10I 0

     C                   Extrct    InDate:*D     nDay
     C                   Extrct    InDate:*M     nMonth
     C                   Extrct    InDate:*Y     nYear
     C                   if        nMonth < 3
     C                   eval      nMonth = nMonth + 12
     C                   eval      nYear  = nYear  -  1
     C                   endif

     C                   Eval      nDayOfWeek = %REM( %int((13*nMonth+3)/5) +
     C                                    nDay + nYear + %int(nYear/4) -
     C                                    %int(nYear/100) + %int(nYear/400)
     C                                    : 7)

      **  The date we return is ones-based and Sunday-oriented (1=Sun, 2=Mon, etc.),
      **  whereas the calculation produces a zero-based and Monday-oriented
      **    (e.g., 0=Mon, 1=Tues, 3=Wed, etc. 7=Sun).
      **  We need to convert the day from the Mon=0-based value to the Sun=1-based value.
     C                   If        nDayOfWeek > 5
     C                   Eval      nDayOfWeek = nDayOfWeek - 5
     C                   else
     C                   eval      nDayOfWeek = nDayOfWeek + 2
     C                   endif
     C                   return    nDayOfWeek
     GetDayEx        E

That's my summer project. What will you do with Zeller's congruence for some summer fun?

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: