Xref: utzoo comp.databases:6820 comp.software-eng:4053 Path: utzoo!attcan!uunet!wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!uflorida!kluge!root!news From: news@root.fiu.edu Newsgroups: comp.databases,comp.software-eng Subject: Re: Calendar Algorithm Keywords: calendar algorithm Message-ID: <1145@kluge.fiu.edu> Date: 10 Aug 90 21:40:36 GMT References: <140352@sun.Eng.Sun.COM> Sender: news@kluge.fiu.edu Followup-To: comp.databases Organization: Florida International University, Miami Lines: 32 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;