Xref: utzoo comp.databases:6781 comp.software-eng:4042 Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!hacgate!ashtate!dbase!awd From: awd@dbase.A-T.COM (Alastair Dallas) Newsgroups: comp.databases,comp.software-eng Subject: Re: Calendar Algorithm Summary: CACM Keywords: calendar algorithm Message-ID: <669@dbase.A-T.COM> Date: 10 Aug 90 03:18:37 GMT References: <140352@sun.Eng.Sun.COM> Organization: Ashton Tate Development Center Glendale, Calif. Lines: 30 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/