From: Jerome Fine <jhfine@idirect.com>
To: Rob Brown <robbrown@shaw.wave.ca>
Subject: Re: Y2K Conversion of Date to Internal Format
Date: 1998/08/19 [thread overview]
Message-ID: <35DB845F.918A044@idirect.com> (raw)
In-Reply-To: 01bdcb38$5fecbf40$2e0f4018@fsys.wave.shaw.ca
>Rob Brown wrote:
> I'm sure Jerome has solved this by now, but ...
> > Jerome Fine <jhfine@idirect.com> 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)
next prev parent reply other threads:[~1998-08-19 4:00 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-08-17 4:00 Y2K Conversion of Date to Internal Format Jerome Fine
1998-08-19 4:00 ` Rob Brown
1998-08-19 4:00 ` Rob Brown
1998-08-19 4:00 ` Jerome Fine [this message]
1998-08-20 4:00 ` Pierre.Bru
[not found] ` <199808201251.HAA30561@x8.dejanews.com>
1998-08-21 4:00 ` Jerome Fine
1998-08-21 4:00 ` D.Webb
1998-08-22 4:00 ` Brian Inglis
1998-08-23 4:00 ` Charles Richmond
1998-08-24 4:00 ` Pierre.Bru
1998-08-24 4:00 ` Martin Vorlaender
-- strict thread matches above, loose matches on Subject: below --
1998-08-15 4:00 Jerome Fine
1998-08-17 4:00 ` Kevin Handy
1998-08-17 4:00 ` Jim Becker
1998-08-20 4:00 ` George Cornelius
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox