From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,287e8ed2b0c0aabf X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-07-29 01:29:14 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!nycmny1-snh1.gtei.net!nycmny1-snf1.gtei.net!news.gtei.net!newsfeed-zh.ip-plus.net!news.ip-plus.net!not-for-mail From: Thomas Wolf Newsgroups: comp.lang.ada Subject: Re: Zeller's Algorithm Date: Mon, 29 Jul 2002 10:29:09 +0200 Organization: --- Message-ID: References: <3D3DED1F.24DE3AC8@lmco.com> <3D3F1C88.B430AC1F@lmco.com> Reply-To: t_wolf@angelfire.com NNTP-Posting-Host: pargate2.paranor.ch X-Trace: rex.ip-plus.net 1027931340 8224 195.65.4.190 (29 Jul 2002 08:29:00 GMT) X-Complaints-To: abuse@ip-plus.net NNTP-Posting-Date: Mon, 29 Jul 2002 08:29:00 +0000 (UTC) X-Newsreader: MicroPlanet Gravity v2.50 Xref: archiver1.google.com comp.lang.ada:27442 Date: 2002-07-29T10:29:09+02:00 List-Id: paul.a.storm@lmco.com wrote: > > I am still confused as to why the Zeller algorithm wouldn't work. Since > the algorithm has been > around since 1875 I figure it has proven out. Although I am still > cogitating on whether it > takes into account for centennial non-leap years(1800,1900, etc.) and > quatra-centennial leap years(i.e. 2000). Yes it does. > It bugs me when I can't figure out the reason for some code not > working. Know what I mean? Zeller's *algorithm* does work for dates in the Gregorian calendar. It's just the poor, buggy *implementation* posted at AdaPower that doesn't work. > Did you try to compile the program from the link I gave and did you have > any problems like I had? No, I didn't try to run that program. It is flawed in two ways: 1. The "if Month < 3" stuff should be moved out of GetValidDate into UseZeller. 2. UseZeller shouldn't add 1 at the end. For a working implementation of Zeller's algorithm, try this: type Weekday is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); -- ISO starts the week on a monday, so let's do the same here... function Day_Of_Week (Date : in Ada.Calendar.Time) return Weekday is -- Returns the day of the week. Works for dates of the -- Gregorian calendar. Day : Ada.Calendar.Day_Number; Month : Ada.Calendar.Month_Number; Year : Ada.Calendar.Year_Number; Secs : Ada.Calendar.Day_Duration; M, Y, Century, Z : Integer; begin -- This is Zeller's formula. Ada.Calendar.Split (Date, Year, Month, Day, Secs); if Month < 3 then M := Integer (Month) + 10; Y := Integer (Year) - 1; else M := Integer (Month) - 2; Y := Integer (Year); end if; Century := Y / 100; Y := Y mod 100; Z := (Integer (Day) + (13 * M - 1) / 5 + Y + Y / 4 + Century / 4 - 2 * Century) mod 7; -- The left operand of the "mod" may be negative, but "mod 7" -- does the right thing and gives us a positive remainder in -- the range 0 .. 6. -- -- Now 0 is Sunday, 1 is Monday, and so on. Map this such that -- 0 is Mon, 1 is Tue, and 6 is Sun. return Weekday'Val ((Z + 6) mod 7); end Day_Of_Week; I'll make available this function in a future release of my Util subsystem. -- ----------------------------------------------------------------- Thomas Wolf e-mail: t_wolf@angelfire.com