From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f7344,47b877165af3dad0 X-Google-Attributes: gidf7344,public X-Google-Thread: 1164f8,47b877165af3dad0 X-Google-Attributes: gid1164f8,public X-Google-Thread: ffc31,47b877165af3dad0 X-Google-Attributes: gidffc31,public X-Google-Thread: fdd80,47b877165af3dad0 X-Google-Attributes: gidfdd80,public From: Jerome Fine Subject: Re: Y2K Conversion of Date to Internal Format Date: 1998/08/19 Message-ID: <35DB845F.918A044@idirect.com> X-Deja-AN: 382848004 Content-Transfer-Encoding: 7bit References: <35D8AD45.458E8C95@idirect.com> <01bdcac2$35061860$7df84018@fsys.wave.shaw.ca> <01bdcb38$5fecbf40$2e0f4018@fsys.wave.shaw.ca> To: Rob Brown Content-Type: text/plain; charset=us-ascii X-Trace: 20 Aug 1998 02:06:09 GMT, ts6-34t-1.idirect.com Organization: "Usenet User" MIME-Version: 1.0 Newsgroups: vmsnet.pdp-11,alt.sys.pdp11,comp.os.vms,comp.sys.dec >Rob Brown wrote: > I'm sure Jerome has solved this by now, but ... > > Jerome Fine wrote in article > > <35D8AD45.458E8C95@idirect.com>... > > > DATE = CONVRT ( MONTH, DAY, YEAR ) > > > MONTH = MONTH - 2 > > > IF ( MONTH .LE. 0 ) MONTH = MONTH + 12; YEAR = YEAR - 1 > > > Q = YEAR / 100 > > > R = YEAR - Q * 100 > > > DATE = ( YEAR - 1910 ) * 365 + Q / 4 + R / 4 + > > > ( MONTH + 1 ) * 28 + ( 26 * > > > MONTH - 2 ) / 10 + DAY > > > The problem obviously has something to do with the 1999 -> > > 2000 transition, but I can't see what it is right now. > > As Kelvin Smith observed in another article, the problem is that when > the year changes from 1999 to 2000, the Q/4 term contributes one more > day, but the R/4 term contributes 24 less. > > The solution is to get rid of Q and R and just use a term containing > YEAR/4. You don't need to worry about the fact that 2100 is not a leap > year. Jerome stated he was using 16 bit integer arithmetic, which > overflows May 31, 2089. > > So the corrected algorithm is: > > MONTH = MONTH - 2 > IF (MONTH .LE. 0) THEN > MONTH = MONTH + 12 > YEAR = YEAR - 1 > ENDIF > DATE = (YEAR-1910)*365 ! 365 days per year > + (YEAR-1910+2)/4 ! leap day every four years > + (MONTH+1)*28 ! amazing days per month calculation > + (26*MONTH-2)/10 > + DAY > + 6 ! magic number to make it all > ! come out right, > ! but coincidentally the number of days > ! from Dec 26 to Jan 1. > > You're welcome. > > - Rob > > -- > > Rob Brown | RSX > G. Michaels Consulting Ltd. | VMS > brown@gmcl.com | Y2K Jerome Fine replies: Thank you very much. To all those individuals who had a hand in the above solution plus, a very helpful explanation as to why the "amazing days per month calculation" works (see his next post by Rob Brown of 1 second later). In addition, you are correct in that I also solved the problem, and in a different manner. It is debatable as to which solution is better or more elegant, but if I may present my solution, the reader can be his own judge. Note that even though my final result was coded for a FORTRAN IV environment where the integers are all signed 16 bits words in a PDP-11 environment (the code was done in machine language since I may only patch the original SAV file), the essence is in this FORTRAN 77 subroutine. I would expect that coding it in FORTRAN IV would yield the same result, but it would not be simple to print it out when IDENC becomes 32768 on 13-Sep-1999. FUNCTION INTEGER*4 IDENC ( MONTH, DAY, YEAR ) INTEGER*2 MONTH, DAY, YEAR, M, Y, YLEAP, DT ( 12 ) INTEGER*4 YLONG DATA DT / 64, 95, 125, 156, 186, 217, 248, 278, 309, 339, 370, 401 / M = MONTH - 2 Y = YEAR IF ( M .LE. 0 ) THEN M = M + 12 Y = Y - 1 ENDIF YLONG = Y YLONG = YLONG * 365 YLEAP = ( Y - 1908 ) / 4 IDENC = YLONG + YLEAP + DT ( M ) + DAY END Notice that this algorithm starts out using the same transformation of placing January and February at the end of the year along with the leap day when it is present. This makes it very easy to account for leap days in a manner which affects the rest of the calculation as little as possible. Thus, the only significant difference in the method is the use of a table to determine the number of days up the the start of the given month. In principle, this method is faster, but takes somewhat more overall code and data. Fortunately, this correct algorithm, as well as the one above, both fit into the same code space as the original incorrect algorithm. I am referring the to assembly language versions. I did not take the trouble to code the other method, but I am assuming that it would also fit in less code space since the Q and R fudge factors have been combined and the calculation should take fewer words. What I can't figure out is why Zeller did not also use a table driven method as I did since in principle it is faster, uses less total memory (than his incorrect procedure) and is much easier to understand. As stated, I believe the algorithm can be easily extended both backward and forward by hundreds of years. What the leap year situation is like after 2100 (which we all agree will not be a leap year), I have no idea. However, if the total number of days covered by the complete range exceeds 65535 days in total, then a 32 bit result would be required, which the above example already provides. In assembler, the code would be almost identical since the PDP-11 already produces a 32 bit output for the multiply instruction. A few minor additions to the rest of the calculation (in PDP-11, a few Adc instructions) and the result is complete. One other addition to this subroutine is the reverse: SUBROUTINE IDDEC ( DATE, MONTH, DAY, YEAR ) If anyone would like the algorithm for this calculation, please send an e-mail. This post is already too long. If enough replies are received, I will post IDDEC in a few days for all to see. Again, the interest is very much appreciated Sincerely yours, Jerome Fine RT-11/TSX-PLUS User/Addict Year 2000 Solutions for Legacy RT-11 Applications (Sources not always required)