Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!tank!shamash!com50!midgard!syntel!dal From: dal@syntel.UUCP (Dale Schumacher) Newsgroups: comp.sys.atari.st Subject: Re: Will Your SW Make it to the year 2000? Summary: Use magic Julian date formula to find leap years Keywords: Julian Date Message-ID: <061089A1311@syntel.UUCP> Date: 10 Jul 89 14:32:24 GMT Reply-To: dal@syntel.UUCP (Dale Schumacher) Lines: 56 X-Member-Of: STdNET (ST Developer's Network) [hjg@amms4.UUCP (Harry Gross) writes...] > In article <381@amms4.UUCP> I wrote: [several leap-year calculations and corrections deleted] > > if (year%4 == 0 && year%100 != 0 || year%400 == 0) > printf("LEAP YEAR!"); > > That test comes from the K & R book, from their date conversion routine > somewhere around page 103 (113?). I should have looked before I leapt :-) Another approach is to use the Julian date to determine if there is a Feb 29th in a certain year. The following is a nice computation for Julian date (from dLibs) which will remain valid far into the future: long julian_date(time) register struct tm *time; /* * Number of days since the base date of the Julian calendar. */ { register long c, y, m, d; y = time->tm_year + 1900; /* year - 1900 */ m = time->tm_mon + 1; /* month, 0..11 */ d = time->tm_mday; /* day, 1..31 */ if(m > 2) m -= 3L; else { m += 9L; y -= 1L; } c = y / 100L; y %= 100L; return( ((146097L * c) >> 2) + ((1461L * y) >> 2) + (((153L * m) + 2) / 5) + d + 1721119L ); } To determine if a given year is a leap year, calculate the Julian date of Feb 28th and March 1st of that year and see if they are 1 or 2 days apart. In addition, since Julian date is a linear count of days, it makes date difference computations much easier. Used with a modulus operation, it also provides an easy way to determine day of the week, like this: t->tm_wday = (jdate + 1) % 7; /* 0=Sunday ... 6=Saturday */ PS. The tricky part is converting back from a Julain date to month/day/year. \\ / Dale Schumacher 399 Beacon Ave. \\ / (alias: Dalnefre') St. Paul, MN 55104-3527 >< ...umn-cs!midgard.mn.org!syntel!dal United States of America / \\ "What is wanted is not the will to believe, but the will to find out, / \\ which is the exact opposite." -Bertrand Russell