Subject: | |
From: | |
Reply To: | |
Date: | Thu, 27 Jun 1996 12:53:52 -0700 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
Re:
> >For example, many people have code that calculates the "day-of-week"
> >for a given date ... using an algorithm that breaks down after 1999-12-31.
...
> I don't mean to seem naive, but could someone provide me a copy of Euler's
> algorithm. To be honest, I reinvented the wheel when trying to come up with a
A quick WWW search via http://www.altavista.digital.com 's advanced
search page, looking for: "day of week" near algorithm resulted in 3 hits...
one bad URL, one that was had a pointer to a "day of week" page somewhere,
and one that had exactly the right info (in a large page).
From the page at http://www.best.com/~mxmora/umpg/UMPG_II_Math&Algorithms.html
here's the appropriate snippet...
----------------------------------------------------------------------------
From: [log in to unmask] (Steve Clamage)
Subject: Re: Algorithm for Day-Of-Week NEEDED
[log in to unmask] (Alan P Barrett) writes:
>
In article <[log in to unmask]>,
>[log in to unmask] (David B. Pickens) writes:
>> index = ((13 * month - 1) / 5) + day + (year % 100) +
>> ((year % 100) / 4) + ((year / 100 / 4) - 2 *
>> (year / 100) + 77;
>> index = index - 7 * (index / 7);
>Why not index = index % 7 ?
>I see that you try to avoid the mistake that I have seen in many
>implementations of Zeller's Congruence in C. If the +77 were not used
>then the initial index calculation could have a negative result, in which
>case the / and % operators would have an implementation defined meaning.
>With the +77, you are safe until the year 4599 (unless they change the
>rules before then), but it breaks on 1 Mar 4600 (month=1, day=1,
year=4600,
>initial index = 2 + 1 + 0 + 0 + 11 - 92 + 77 = -1).
Here is a C version of Zeller's Congruence which doesn't have that problem:
The day of the week 'W' (0 = Sunday) is given by
W = ((13*M - 1) / 5 + D + Y + Y/4 + C/4 - (2*C)%7 + 7) % 7;
where
C is the century (year/100)
Y is the last two digits of the year (year%100)
D is the day of the month
M is a special month number, where Jan and Feb are taken as
month 11 and 12 of the previous year (subtract 1 from the year)
and each division is truncating division, and cannot be combined.
The properties of modulo arithmetic are such that we may apply it
individually to each term of a sum. To guarantee that our sum is positive,
we can find (2*C)%7, which is subtracted instead of 2*C, and add an
additional 7 before the final %7.
---------------------------------end cut-----------------------------------
I haven't tried the code.
--
Stan Sieler [log in to unmask]
http://www.allegro.com/sieler.html
|
|
|