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: ffc31,47b877165af3dad0 X-Google-Attributes: gidffc31,public X-Google-Thread: 1164f8,47b877165af3dad0 X-Google-Attributes: gid1164f8,public X-Google-Thread: fdd80,47b877165af3dad0 X-Google-Attributes: gidfdd80,public From: cornelius@eisner.decus.org (George Cornelius) Subject: Re: Y2K Conversion of Date to Internal Format Date: 1998/08/20 Message-ID: <1998Aug20.015217.1@eisner>#1/1 X-Deja-AN: 382902778 X-Nntp-Posting-Host: eisner.decus.org References: <35D63BEB.2A9533BB@idirect.com> X-Trace: news.decus.org 903592340 17093 CORNELIUS [192.67.173.2] Organization: DECUServe Newsgroups: vmsnet.pdp-11,alt.sys.pdp11,comp.os.vms,comp.sys.dec In article <35D63BEB.2A9533BB@idirect.com>, Jerome Fine writes: > I am in the process of converting some application > programs which have been written in FORTRAN. > The complete package uses an internal DATE value > which starts on 26-Dec-1909 as 0 and increases for > each day. The algorithm which is broken is: > 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 What's this, Zoeller's congruence with a year-2000 bug thrown in for good measure? The algorithm is dead wrong on century year calculations. (It also is missing the +DAY term, but I'll assume that's just a typo). I'll rewrite your calculation as (YEAR-1910)*365 + YEAR/400 + REM(YEAR,100)/4 + 30*MONTH + (6*MONTH-2)/10 + DAY + 28 , where REM is "remainder when divided by". The first line tries, incorrectly, to compute an offset to "first day" (March 1) of year, the second, offset from that point to first day of month, the third to day of month. REM(YEAR,100)/4 has a discontinuity (changes by -24) when it hits any century year, and this is the source of the problem. For the first line try something like this instead: (YEAR-1910)*365 + YEAR/4 - YEAR/100 + YEAR/400 (Every 4th year is a leap year, except every century year isn't, except every multiple of 400 is). In fact, in your case (16 bit unsigned result), the only century year you will encounter is year 2000, so you can drop the last two terms of this line entirely. Either way you will have to make an adjustment to the constant term in order to have the desired base date. -- George Cornelius cornelius@eisner.decus.org cornelius@mayo.edu