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.
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 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 eval nDayOfWeek = nDayOfWeek + 2
C return nDayOfWeek
P 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.