[comp.software-eng] Calendar Algorithm

jasonf@cetemp.Eng.Sun.COM (Jason Freund) (08/08/90)

	I need to be able to change dates in a database cell with commands like
"+4w" == "Find the closest weekday to the day 4 weeks from a certain date", 
"-12d" == "Find the closest weekday 12 days earlier than this date.", and so 
on.  I'll be writing in Pascal -- the code is no problem -- I'll give my 
function a "+/-", a number, and a "d/w/m" (day, week, month), and a date 
"MMDDYY".  The function returns the closes weekday by adding/subtracting 
according to its parameters.  Does anyone know of a general algorithm for 
dealing with calendars?  It must that take into account leap years and be able 
to find what day of the week it is (MTWRF) so that it can make approximations 
to weekdays. I've seen some good ones before, (for computing how many days old 
you are and what day of the week you were born on) but I can't find any now.  

BTW, when is the next leap year?

Thanks,
Jason Freund, Sun Microsystems,  jasonf@cetemp.Corp.sun.com  <== summer address
Deprtmnt of Computer Science, Univ California, Davis. freund@sakura.ucdavis.edu
Quantum Link: JasonF5,  Compu$erve: 72007,244, 690 Erie Dr, Sunnyvale, CA 94087
-------------------------------------------------------------------------------
STOLEN QUOTES -- Please give the authors credit if you know who they are!    
"To understand recursion, you need to understand recursion."
"Wow!  Virtual memory!  Now I'm gonna build me a REALLY big ram disk!"
"My other computer is a SUN3/50."  "E. Pluribus UNIX"   -- authors unkown 

awd@dbase.A-T.COM (Alastair Dallas) (08/10/90)

In article <140352@sun.Eng.Sun.COM>, jasonf@cetemp.Eng.Sun.COM (Jason Freund) writes:
> 	I need to be able to change dates in a database cell with commands like
> "+4w" == "Find the closest weekday to the day 4 weeks from a certain date", 
> "-12d" == "Find the closest weekday 12 days earlier than this date.", and so 
> on.  I'll be writing in Pascal -- the code is no problem -- I'll give my 
> function a "+/-", a number, and a "d/w/m" (day, week, month), and a date 
> "MMDDYY".  The function returns the closes weekday by adding/subtracting 
> according to its parameters.  Does anyone know of a general algorithm for 
> dealing with calendars?  It must that take into account leap years and be able 
> to find what day of the week it is (MTWRF) so that it can make approximations 
> to weekdays. I've seen some good ones before, (for computing how many days old 
> you are and what day of the week you were born on) but I can't find any now.  

The collected algorithms of the ACM has an algorithm for converting
m-d-y into an integer number of days from some start point a long time
ago, and back again.  That makes it easy to add days or weeks.  For
months, you're pretty much stuck adding to your m-d-y months and 
dealing with overflow into years.  The ACM book describes the weekday
values for the resulting integer modulus 7.  (That is, if no_of_days % 7
equals 0, it's Sunday, or Saturday, or something like that.)

> BTW, when is the next leap year?

That's easy.  Leap years occur every four years, except years evenly
divisible by 100.  The last exeception is that years evenly divisible
by 400 _are_ leap years.  Therefore, '92 and '96 are leap years and
for the first time in four hundred years, the turn of the century will
be a leap year.  All because the solar year isn't exactly 365-1/4 days.

/alastair/

news@root.fiu.edu (08/11/90)

In article <140352@sun.Eng.Sun.COM> jasonf@cetemp.Eng.Sun.COM (Jason Freund) writes:
>
>	I need to be able to change dates in a database cell with commands like
>"+4w" == "Find the closest weekday to the day 4 weeks from a certain date", 
>"-12d" == "Find the closest weekday 12 days earlier than this date.", and so 
>on.  I'll be writing in Pascal -- the code is no problem -- I'll give my 
>function a "+/-", a number, and a "d/w/m" (day, week, month), and a date 
>"MMDDYY".  The function returns the closes weekday by adding/subtracting 
>according to its parameters.  Does anyone know of a general algorithm for 
>dealing with calendars?  It must that take into account leap years and be able 
>to find what day of the week it is (MTWRF) so that it can make approximations 
>to weekdays. I've seen some good ones before, (for computing how many days old 
>you are and what day of the week you were born on) but I can't find any now.  
>


   function canonical_day_number( date: YMD_date ) return integer is
      -- Expects a year, month, day combination and returns an integer
      -- which is the number of days since a very long time ago.  To
      -- find the number of days between two dates, subtract these numbers.
      -- To find the day of the week, take the result mod 7.
      year2  : integer := date.year;
      month2 : integer := date.month;
   begin
      if month2 < 3 then 
         month2 := month2 + 12; 
         year2 := year2 - 1; 
      end if;
      -- The following code avoids using floating point or really big numbers.
      return year2 * 365 + year2 / 4 - year2 / 100 + year2 / 400
            +( month2 * 306 + 17 )/ 10 + date.day;
   end canonical_day_number;

koerberm@nixsin.UUCP (Mathias Koerber) (08/15/90)

Howdy,

In article <669@dbase.A-T.COM> awd@dbase.A-T.COM (Alastair Dallas) writes:
>In article <140352@sun.Eng.Sun.COM>, jasonf@cetemp.Eng.Sun.COM (Jason Freund) writes:
>> 	I need to be able to change dates in a database cell with commands like
>> "+4w" == "Find the closest weekday to the day 4 weeks from a certain date", 
[... stuff deleted ]
>The collected algorithms of the ACM has an algorithm for converting
[...]
>dealing with overflow into years.  The ACM book describes the weekday

What is the ACM (guess: American Computing Monthly?,A magazine or so?).
And what about the book?. Could anyone please e-mail me a short description
of what it contains, and maybe an ISBN, price etc?

Thx,

Mathias

-- 
Mathias Koerber           |Tel:   +65 / 7473828 ext 1852|Fax: +65 / 7474331
Nixdorf Computer Singapore|EUnet: koerber.sin@nixpbe    |nerv:  koerber.sin
2 Kallang Sector          |uunet: uunet!linus!nixbur!koerber.sin
Singapore 1334            |[Standard-disclaimer:All views personal...     ]

mitchell@chance.uucp (George Mitchell) (08/27/90)

In article <1115@nixsin.UUCP> koerberm@nixsin.UUCP (Mathias Koerber) writes:
`What is the ACM (guess: American Computing Monthly?,A magazine or so?).
`And what about the book?. Could anyone please e-mail me a short description
`of what it contains, and maybe an ISBN, price etc?

Would it be appropriate to post a periodic (monthly?) welcome
message for comp.software.eng that contains some pointers to
news.announce.newusers and provides some of the information we
expect most readers (and more posters) to possess?
--
George Mitchell, MITRE, MS Z676, 7525 Colshire Dr, McLean, VA  22102
email: gmitchel@mitre.org  [alt: mitchell@community-chest.mitre.org]
vmail: 703/883-6029         FAX: 703/883-5519

kirsch@hpcc01.HP.COM (Hans-Joachim Kirsch) (08/27/90)

I also would like to now what  ACM  means and whether there is a standard
book available that describes such date (or similiar) algorythms.
Please e-mail or post here.

Hans-Joachim Kirsch 

--------------------------------------------------------------------------------
Hewlett-Packard Company / CQIS         |
Voice: +(415) 857-7126                 |   This space needs to be filled with
e-mail: kirsch@hpcc01.corp.hp.com      |              knowledge!
--------------------------------------------------------------------------------

mcgregor@hemlock.Atherton.COM (Scott McGregor) (08/28/90)

> What is the ACM (guess: American Computing Monthly?,A magazine or so?).
> And what about the book?. Could anyone please e-mail me a short description
> of what it contains, and maybe an ISBN, price etc?


ACM is the Association for Computer Machinery.  It is one of the two
major professional societies for people working in the computer
field.  (The IEEE (Institute of Electrical and Electronics Engineers)
Computer Society is the other major professional society).  Both
societies publish
many important books (such as Collected Algorithms mentioned above) and
magazines (such as Communications of the ACM, IEEE Computer, IEEE
Software...).  They sponsor many conferences and special interest groups
(SIGs).  Both are based in the US, and sponsor most activities there,
but have international arms.  Much time and effort can be saved be
reusing work already
done by others and published by these societies.  Good technical libraries
well have many of their publications available, so they are accessible if
you want them, probably even in Singapore.

Note, I posted this rather than replying privately, because I have
found a number of US educated engineers don't know about or refer to the
Collected Algorithms of the ACM and other useful ACM and IEEE books
and journals.  I believe that this may be a defect in our current
computer science engineering programs, where we often encourage reinvention
rather than library research and and reuse.  Later when these engineers
move from the halls of academia into industry becomes the dreaded
Not Invented Here syndrome that undermines reuse strategies).  The number
of people who are unfamiliar with Ivan Sutherland's Sketchpad, Doug
Engelbart's NLS Augment system, and other contributions of the 60s is
staggering, yet many people are trying to reinvent this same technology
today.  A historical perspective can often help one to develop "new"
technology more quickly.  We should at least be thankful that the
many people are once again "rediscovering" virtual memory--now for personal
computers.

Scott McGregor 

norm@oglvee.UUCP (Norman Joseph) (08/29/90)

In <1115@nixsin.UUCP> koerberm@nixsin.UUCP (Mathias Koerber) writes:

>>The collected algorithms of the ACM has an algorithm for converting [...]

>What is the ACM (guess: American Computing Monthly?,A magazine or so?).

ACM is The Association for Computing Machinery,  a professional society
of the computing community founded in 1947.  For membership information,
write to their headquarters:

        ACM
        Membership and Marketing Services
        11 West 42nd Street
        New York, NY  10036
        Tel: 212 869-7440
        Telex: 421686

>And what about the book?.

The Collected Algorithms of the ACM are in four volumes, with updates
quarterly, and costs US $150 for ACM members.
-- 
Norm Joseph - (amanue!oglvee!norm)           cgh!amanue!oglvee!norm@dsi.com, or
  Oglevee Computer Systems, Inc.             ditka!oglvee!norm@daver.bungi.com
                                   ---<*>---
      "Shucking Usenet oysters in pursuit of a pearl."  --  Bill Kennedy

torkil@Pacesetter.COM (Torkil Hammer) (08/31/90)

# 
# The Collected Algorithms of the ACM are in four volumes, with updates
# quarterly, and costs US $150 for ACM members.

I really hope that the ACM algorithm is identical to the Julian Day Number
used by astronomers.  We don't need another day by day calendar.

Mon, Jan 1, 1990 in the gregorian calendar we use is JD 2447893, though
the definition really is that JD 0 is Mon, Jan 1, 4713 BC in the (proleptic)
julian calendar, that was used before the gregorian.  Specifically JD 0.0 is
defined to be noon GMT on that day.

Can anyone with knowledge of the ACM algorithm confirm?

Torkil Hammer