Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Y2K Conversion of Date to Internal Format
@ 1998-08-15  4:00 Jerome Fine
  1998-08-17  4:00 ` Jim Becker
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Jerome Fine @ 1998-08-15  4:00 UTC (permalink / raw)


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
where all variables are 16 bit integers.  Obviously the value overflows and
becomes negative on 13-Sep-1999 which is exactly what is wanted.

I have taken a few liberties with the FORTRAN code since I wanted
to make the algorithm clear.

The problem is that the algorithm BREAKS on 01-Mar-2000 at which
point the DATE value reverts to the same value as for 06-Feb-2000
and 24 duplicates are created.  There is also a routine which works in
the opposite direction which just ignores the first set of duplicate days
from 06-Feb-2000 to 29-Feb-2000.

(A)  Is anyone familiar with an algorithm which works to convert
        to the internal format and in the reverse direction?
(B)  The size is important since I MUST patch the application
        programs, NOT compile and link.

I suspect that someone thought we should revert to the old
calendar when those 11? days were lost and give everyone a
change to live 24 days a second time.  Anyone remember
the names of the old and the present calendar which was
modified to take into account those extra leap years?  I
though a bit of humour might make the question a bit
more interesting?

Sincerely yours,

Jerome Fine
RT-11/TSX-PLUS  User/Addict
Year 2000 Solutions for Legacy RT-11 Applications
(Sources not always required)




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
@ 1998-08-17  4:00 Jerome Fine
  1998-08-19  4:00 ` Rob Brown
  0 siblings, 1 reply; 15+ messages in thread
From: Jerome Fine @ 1998-08-17  4:00 UTC (permalink / raw)


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
where all variables are 16 bit integers.  Obviously the value overflows and
becomes negative on 13-Sep-1999 which is exactly what is wanted.

I have taken a few liberties with the FORTRAN code since I wanted
to make the algorithm clear.

The problem is that the algorithm BREAKS on 01-Mar-2000 at which
point the DATE value reverts to the same value as for 06-Feb-2000
and 24 duplicates are created.  There is also a routine which works in
the opposite direction which just ignores the first set of duplicate days
from 06-Feb-2000 to 29-Feb-2000.

(A)  Is anyone familiar with an algorithm which works to convert
        to the internal format and in the reverse direction?
(B)  The size is important since I MUST patch the application
        programs, NOT compile and link.

I suspect that someone thought we should revert to the
old Julian Calendar as opposed to the current Gregorian
calendar when those 11? days were lost and give everyone a
change to live 24 days a second time.   I though a bit of
humour might make the question a bit more interesting?

I have added this portion of my request and modified the
paragraph above in order to clarify things.  First, although
the algorithm will obviously be of a general nature, the actual
implementation will be on a PDP-11 running a program
under TSX-PLUS.  The final language used to do the
implementation will be coding in machine language since
the changes will be PATCHES to an existing program.
I do intend to use assembler (or MACRO) to do some
of the testing.  SO, no VMS (nor for that matter any other
operating system dependent interactions) are allowed.
This is, as expressed above, the necessity for a substitute
algorithm for the current on which breaks on 06-Feb-2000.
The inputs are:  MONTH,   DAY,   YEAR
The output is:  Number of days since 26-Dec-1909.
Note that on 12-Sep-1999, this value is 32767.  On the
next day, the value overflows (from a technical point of
view) and becomes 32768.  That value does not exist as
far as FORTRAN  IV  is concerned since integers range
from -32768 to +32767.  That does not mean that the
correct UNSIGNED value cannot be returned from
a program written in machine language (MACRO)
and used for subsequent calculations (although it is
recognized that in some cases the FORTRAN generated
instructions which use signed branches must be changed
to unsigned branches - already done once when an
expiry date of 31-Dec-1999 was required).

Comments on the algorithm, although interesting, are
not as appreciated as would a new algorithm which
works.  Incidentally, the reverse algorithm must also
be produced since it is absolutely necessary to
convert from the internal format to M/D/Y.

Sincerely yours,

Jerome Fine
RT-11/TSX-PLUS  User/Addict
Year 2000 Solutions for Legacy RT-11 Applications
(Sources not always required)




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-15  4:00 Jerome Fine
  1998-08-17  4:00 ` Jim Becker
@ 1998-08-17  4:00 ` Kevin Handy
  1998-08-20  4:00 ` George Cornelius
  2 siblings, 0 replies; 15+ messages in thread
From: Kevin Handy @ 1998-08-17  4:00 UTC (permalink / raw)
  To: Jerome Fine



Jerome Fine wrote:

> 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 )

I am assuming this is the call to the following code.

> 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
> where all variables are 16 bit integers.  Obviously the value overflows and
> becomes negative on 13-Sep-1999 which is exactly what is wanted.

I don't see it overflowing, unless I am missing something obvious.As long as you pass
in the full year, it looks like it should work to me.
It appears to be trying to calculate the julian date for the first day of
the month, or something approximately like it.

> I have taken a few liberties with the FORTRAN code since I wanted
> to make the algorithm clear.
>
> The problem is that the algorithm BREAKS on 01-Mar-2000 at which
> point the DATE value reverts to the same value as for 06-Feb-2000
> and 24 duplicates are created.  There is also a routine which works in
> the opposite direction which just ignores the first set of duplicate days
> from 06-Feb-2000 to 29-Feb-2000.

I'd suspect you are overflowing an integer.

    (2000 - 1910) * 365 = 32850

Where a 16 bit signede integer is limited to -32768 to 3767
Probibly overflowed in the year

    (32767 / 365) + 1910 = 1999.77

ie. Somewhere in the 3rd quarter of 1999.

My only suggestion is to change the base year from 1910 to
something later, but it will break any existing data is
is stored in a file. If you are not storing it in a file, than I'd
look at the dates it needs to process and set it to the
earliest that you need to handle. It depends on what you are
doing with the number returned.

> (A)  Is anyone familiar with an algorithm which works to convert
>         to the internal format and in the reverse direction?
> (B)  The size is important since I MUST patch the application
>         programs, NOT compile and link.
>
> I suspect that someone thought we should revert to the old
> calendar when those 11? days were lost and give everyone a
> change to live 24 days a second time.  Anyone remember
> the names of the old and the present calendar which was
> modified to take into account those extra leap years?  I
> though a bit of humour might make the question a bit
> more interesting?
>
> Sincerely yours,
>
> Jerome Fine
> RT-11/TSX-PLUS  User/Addict
> Year 2000 Solutions for Legacy RT-11 Applications
> (Sources not always required)






^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-15  4:00 Jerome Fine
@ 1998-08-17  4:00 ` Jim Becker
  1998-08-17  4:00 ` Kevin Handy
  1998-08-20  4:00 ` George Cornelius
  2 siblings, 0 replies; 15+ messages in thread
From: Jim Becker @ 1998-08-17  4:00 UTC (permalink / raw)


Jerome Fine wrote:
[snip]
> I suspect that someone thought we should revert to the old
> calendar when those 11? days were lost and give everyone a
> change to live 24 days a second time.  Anyone remember
> the names of the old and the present calendar which was
> modified to take into account those extra leap years?  I
> though a bit of humour might make the question a bit
> more interesting?

See
http://www.openvms.digital.com/openvms/products/year-2000/leap.html

Jim Becker
System Solutions Incorporated (http://www.syssol.com)
Rockville, MD & Herndon, VA

DECUS ESILUG (http://www.decus.org/decus/lugs/esilug)
Washington, DC area



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  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
  0 siblings, 1 reply; 15+ messages in thread
From: Rob Brown @ 1998-08-19  4:00 UTC (permalink / raw)




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 is missing from the DATE formula as shown here, but that is not
really relevant to the problem at hand.

> The problem is that the algorithm BREAKS on 01-Mar-2000 at which
> point the DATE value reverts to the same value as for 06-Feb-2000
> and 24 duplicates are created.

March 1 2000 is the first day that 2000 rather than 1999 is used in
the DATE formula, and date that Q/4 changes value.  (26*MONTH-2)
equals 24 on that date, although I can't see what that has to do with
anything.  The problem obviously has something to do with the 1999 ->
2000 transition, but I can't see what it is right now.  I'm sure the
formula is fixable but I don't have time to look at it again until
tonight.

Hope this helps

- Rob



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-19  4:00 ` Rob Brown
@ 1998-08-19  4:00   ` Rob Brown
  1998-08-19  4:00     ` Jerome Fine
  0 siblings, 1 reply; 15+ messages in thread
From: Rob Brown @ 1998-08-19  4:00 UTC (permalink / raw)



I'm sure Jerome has solved this by now, but ...

Rob Brown <robbrown@shaw.wave.ca> wrote in article
<01bdcac2$35061860$7df84018@fsys.wave.shaw.ca>...
> 
> 
> 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

>            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



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-19  4:00   ` Rob Brown
@ 1998-08-19  4:00     ` Jerome Fine
  1998-08-20  4:00       ` Pierre.Bru
       [not found]       ` <199808201251.HAA30561@x8.dejanews.com>
  0 siblings, 2 replies; 15+ messages in thread
From: Jerome Fine @ 1998-08-19  4:00 UTC (permalink / raw)
  To: Rob Brown

>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)




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-19  4:00     ` Jerome Fine
@ 1998-08-20  4:00       ` Pierre.Bru
       [not found]       ` <199808201251.HAA30561@x8.dejanews.com>
  1 sibling, 0 replies; 15+ messages in thread
From: Pierre.Bru @ 1998-08-20  4:00 UTC (permalink / raw)
  To: jhfine

here is a DCL version of the R.G. Tantzen algorithm that convert gregorian
date to julian day number and back. all is integer arithmetic and 32-bit
integers.

this algorithm is year-2000-proof, leap-year-proof, century-leap-year-proof
and you can compute julain day with this algorithm back to the begining of
the gregorian calendar, october 15, 1582.

I can give you explanations for all these 'magic' numbers if you want.

Pierre.

$!------------------------------------------------------------------------------
$!
$! d/m/y -> julian
$
$ if m.gt.2
$ then
$       m = m - 3
$ else
$       m = m + 9
$       y = y - 1
$ endif
$
$ c = y / 100
$ y = y - 100*c
$
$ j = c*146097/4 + y*1461/4 + (m*153 + 2)/5 + d + 1721119
$
$!------------------------------------------------------------------------------
$ write sys$output "julian day: ''j'"
$!------------------------------------------------------------------------------
$!
$! julian -> d/m/y
$
$ j = j - 1721119
$
$ y = (4*j - 1)/146097
$ j = (4*j - 1)-146097*y
$ d = j/4
$
$ j = (4*d + 3)/1461
$ d = (4*d + 3)-1461*j
$ d = (d+4)/4
$
$ m = (5*d - 3)/153
$ d = (5*d - 3)-153*m
$ d = (d+5)/5
$
$ y = y*100 + j
$
$ if m.lt.10
$ then
$       m = m + 3
$ else
$       m = m - 9
$       y = y + 1
$ endif
$
$!------------------------------------------------------------------------------
$ write sys$output "''d' ''m' ''y'"
$!------------------------------------------------------------------------------

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-15  4:00 Jerome Fine
  1998-08-17  4:00 ` Jim Becker
  1998-08-17  4:00 ` Kevin Handy
@ 1998-08-20  4:00 ` George Cornelius
  2 siblings, 0 replies; 15+ messages in thread
From: George Cornelius @ 1998-08-20  4:00 UTC (permalink / raw)


In article <35D63BEB.2A9533BB@idirect.com>, Jerome Fine <jhfine@idirect.com> 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



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
       [not found]       ` <199808201251.HAA30561@x8.dejanews.com>
@ 1998-08-21  4:00         ` Jerome Fine
  1998-08-21  4:00           ` D.Webb
                             ` (4 more replies)
  0 siblings, 5 replies; 15+ messages in thread
From: Jerome Fine @ 1998-08-21  4:00 UTC (permalink / raw)
  To: Pierre.Bru

>Pierre.Bru@spotimage.fr wrote:

> here is a DCL version of the R.G. Tantzen algorithm that convert gregorian
> date to julian day number and back. all is integer arithmetic and 32-bit
> integers.

Jerome Fine replies:

Thank you very much.  Do you have a reference for this algorithm?
Very often, the source of some information leads to some more that
is also useful.

> this algorithm is year-2000-proof, leap-year-proof, century-leap-year-proof
> and you can compute julain day with this algorithm back to the begining of
> the gregorian calendar, october 15, 1582.

The best information I have here is the start date of the Gregorian Calendar!!!
I presume that 1600, 1700, 1800 and 1900 were all non-leap years.
And from the algorithm, it would seem that every future century is also a
non-leap year except when it is a millennium.  Just how far does that rule
hold in the future and might the earth's rotation be slowing enough such
that in the future, some millennium will also no longer be a leap year?

> I can give you explanations for all these 'magic' numbers if you want.

Thank you.  Rob Brown was able to do so already.  If you have a different
interpretation, please add to the explanation.

> Pierre.
>
> $!------------------------------------------------------------------------------
> $!
> $! d/m/y -> julian
> $
> $ if m.gt.2
> $ then
> $       m = m - 3
> $ else
> $       m = m + 9
> $       y = y - 1
> $ endif
> $
> $ c = y / 100
> $ y = y - 100*c
> $
> $ j = c*146097/4 + y*1461/4 + (m*153 + 2)/5 + d + 1721119
> $
> $!------------------------------------------------------------------------------
> $ write sys$output "julian day: ''j'"
> $!------------------------------------------------------------------------------
> $!
> $! julian -> d/m/y
> $
> $ j = j - 1721119
> $
> $ y = (4*j - 1)/146097
> $ j = (4*j - 1)-146097*y
> $ d = j/4
> $
> $ j = (4*d + 3)/1461
> $ d = (4*d + 3)-1461*j
> $ d = (d+4)/4
> $
> $ m = (5*d - 3)/153
> $ d = (5*d - 3)-153*m
> $ d = (d+5)/5
> $
> $ y = y*100 + j
> $
> $ if m.lt.10
> $ then
> $       m = m + 3
> $ else
> $       m = m - 9
> $       y = y + 1
> $ endif
> $
> $!------------------------------------------------------------------------------
> $ write sys$output "''d' ''m' ''y'"
> $!------------------------------------------------------------------------------

The above algorithms seem just fine.  But, unfortunately, although they
are obviously great for the problem they solve, I cannot use either for
the following 2 reasons:
(a) The base year is not 26-Dec-1909 and the application programs
      must keep compatible with the existing data base,  and
(b)  They have too much code to fit in the same space as the original
       algorithm which broke on 05-Feb-2000.
In addition, as a minor aspect, the algorithm which I suggested in my post
is likely to be a fair bit faster and use less memory.  While it would need
a bit of tweaking to successfully cross 2000 from 1582, as well as not
having leap years for the centuries (I think I just repeated myself), the
use of a table driven method seems to be able to yield a faster answer.
If anyone would like a solution starting with 15-Oct-1582 to the
post I gave on 19-Aug-1998  22:05:19-04:00, I will try and re-work
my calculation.  The key point is that in my calculation, there are many
fewer divisions and multiplication.  Also, all the arithmetic can be
carried out in 16 bits except for the last step; thus only 2 more
instructions would be required to have a 32 bit result starting on
15-Oct-1582, plus the fudge factor because the centuries are
not leap years (complicated because 2000 is a leap year)
something like:   (  15  -  Y / 100  )  +  (  Y / 1000 - 1 )  which
becomes:  Y  /  1000  -  Y  /  100  + 14
Obviously, the table of days,  DT,  would change as well.
Just thinking on paper how my algorithm would change.

I also have an algorithm for the conversion of the julian date
to the M/D/Y.  This algorithm is table driven and requires a
loop to determine the month via repeated comparisons.
It was just finished this morning and I haven't yet converted
it to assembler, but only 1 divide is required and one
multiply (and a shift for a divide by 4 for leap days).  The
final loop to determine the month should require only 3
instructions a maximum of 12 tries.  Since the only
arithmetic operation in the loop is a compare, the loop
should operate more quickly than a divide instruction,
at least on a PDP-11 since the average number of instructions
that will be executed in the loop is only 18.  Note that I
expect that PIC can be used for both replacement subroutines
are that it will all fit into the same space as before.

Sincerely yours,

Jerome Fine
RT-11/TSX-PLUS  User/Addict
Year  2000  Solutions  for  All  RT-11/TSX-PLUS  Applications
(Sources not always required)




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-21  4:00         ` Jerome Fine
@ 1998-08-21  4:00           ` D.Webb
  1998-08-22  4:00           ` Brian Inglis
                             ` (3 subsequent siblings)
  4 siblings, 0 replies; 15+ messages in thread
From: D.Webb @ 1998-08-21  4:00 UTC (permalink / raw)


In article <35DD8F5C.ED2765B7@idirect.com>, Jerome Fine <jhfine@idirect.com> writes:
>
>The best information I have here is the start date of the Gregorian Calendar!!!
>I presume that 1600, 1700, 1800 and 1900 were all non-leap years.
>And from the algorithm, it would seem that every future century is also a
>non-leap year except when it is a millennium.  Just how far does that rule
>hold in the future and might the earth's rotation be slowing enough such
>that in the future, some millennium will also no longer be a leap year?
>
>> I can give you explanations for all these 'magic' numbers if you want.
>
   My understanding is that every century which is a multiple of 400 is a 
   leap year so that, as well as 2000, 1600 was a leap year.
   The year 3000 will not be a leap year.
   The years 2400 and 2800 will be leap years.





David Webb
VMS and Unix team leader
Middlesex University


       




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  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
                             ` (2 subsequent siblings)
  4 siblings, 0 replies; 15+ messages in thread
From: Brian Inglis @ 1998-08-22  4:00 UTC (permalink / raw)


On Fri, 21 Aug 1998 11:16:45 -0400, Jerome Fine <jhfine@idirect.com>
wrote:
[snip]
>I presume that 1600, 1700, 1800 and 1900 were all non-leap years.
>And from the algorithm, it would seem that every future century is also a
>non-leap year except when it is a millennium.  Just how far does that rule
>hold in the future and might the earth's rotation be slowing enough such
>that in the future, some millennium will also no longer be a leap year?
Jerome,
The Gregorian leap year rule is every year evenly divisible by four,
except if evenly divisible by 100, unless also evenly divisible by 400:
so 2000 is a leap year, 1900 and 2100 are not!
Hopefully we will have adopted a more rational calendar before the next
leap century (2400) comes around.
>The best information I have here is the start date of the Gregorian Calendar!!!
The Gregorian calendar was adopted by different countries at different
times, from the date above, when it was adopted by European Catholic
countries, up to this century, when it was adopted by the USSR, Turkey,
Japan and other Asian countries as the official business calendar. 
It was not adopted by Great Britain and its colonies (the U.S. and most
of the English speaking world) until September 1752, so 1700 *was* a
leap year, in our (Julian) epoch, but 1800 was not, in the new
(Gregorian) epoch.
>Jerome Fine
>RT-11/TSX-PLUS  User/Addict
>Year  2000  Solutions  for  All  RT-11/TSX-PLUS  Applications
>(Sources not always required)
Take care. Thanks, Brian Inglis (Calgary, Alberta, Canada) Brian.dot.Inglis.at@CADvision.dot.com



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  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
  4 siblings, 0 replies; 15+ messages in thread
From: Charles Richmond @ 1998-08-23  4:00 UTC (permalink / raw)
  To: Jerome Fine

Jerome Fine wrote:
> 
> >Pierre.Bru@spotimage.fr wrote:
> 
> > here is a DCL version of the R.G. Tantzen algorithm that convert gregorian
> > date to julian day number and back. all is integer arithmetic and 32-bit
> > integers.
> 
> Jerome Fine replies:
> 
> Thank you very much.  Do you have a reference for this algorithm?
> Very often, the source of some information leads to some more that
> is also useful.
> 
I believe this is Algorithm 199 in the _Collected Algorithms of the CACM_, bu
Robert G. Tantzen.  If you do *not* have a good technical library handy, you
can get your local public library to order it via inter-library loan.  (At
least in the U.S.)  This takes a few weeks, but it worth it for something you
*really* need.

-- 
+-------------------------------------------------------------+
|     Charles and Francis Richmond     <richmond@plano.net>   |
+-------------------------------------------------------------+



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-21  4:00         ` Jerome Fine
                             ` (2 preceding siblings ...)
  1998-08-23  4:00           ` Charles Richmond
@ 1998-08-24  4:00           ` Pierre.Bru
  1998-08-24  4:00           ` Martin Vorlaender
  4 siblings, 0 replies; 15+ messages in thread
From: Pierre.Bru @ 1998-08-24  4:00 UTC (permalink / raw)


In article <35DD8F5C.ED2765B7@idirect.com>,
  Jerome Fine <jhfine@idirect.com> wrote:
> >Pierre.Bru@spotimage.fr wrote:
> Jerome Fine replies:
>
> Thank you very much.  Do you have a reference for this algorithm?
> Very often, the source of some information leads to some more that
> is also useful.

this is 'ALGORITHM 199' of 'Association for Computiong Machinery' (ACM)

http://www.math.utah.edu/ftp/pub/tex/bib/toc/cacm1960.html
ftp://ftp.math.utah.edu/pub/tex/bib/cacm1960.html#Tantzen:1963:AAC

[...]
> > I can give you explanations for all these 'magic' numbers if you want.
>
> Thank you.  Rob Brown was able to do so already.  If you have a different
> interpretation, please add to the explanation.

I mean the magic-numbers in the posted DCL algorithm, not in yours.

[...]
> The above algorithms seem just fine.  But, unfortunately, although they
> are obviously great for the problem they solve, I cannot use either for
> the following 2 reasons:
> (a) The base year is not 26-Dec-1909 and the application programs
>       must keep compatible with the existing data base,  and

I know. this is a general algorithm to compute the julian day number from any
gregorain date. if you want the base date be 26-Dec-1909, compute the julian
day number for 26-Dec-1909, and substract this value from the result.

> (b)  They have too much code to fit in the same space as the original
>        algorithm which broke on 05-Feb-2000.

too much code ? the computation 'd/m/y'->'julian' is just :

$ if m.gt.2
$ then
$       m = m - 3
$ else
$       m = m + 9
$       y = y - 1
$ endif
$
$ c = y / 100
$ y = y - 100*c
$
$ j = c*146097/4 + y*1461/4 + (m*153 + 2)/5 + d + 1721119

the "big" part is for 'julian'->'d/m/y' ...
it can easilly be re-worked to be table-driven.

I'm not trying in any way to advocate this algorithm. I've just posted it
because I thouth it could help you to solve your problem.

Pierre.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Y2K Conversion of Date to Internal Format
  1998-08-21  4:00         ` Jerome Fine
                             ` (3 preceding siblings ...)
  1998-08-24  4:00           ` Pierre.Bru
@ 1998-08-24  4:00           ` Martin Vorlaender
  4 siblings, 0 replies; 15+ messages in thread
From: Martin Vorlaender @ 1998-08-24  4:00 UTC (permalink / raw)


Jerome Fine (jhfine@idirect.com) wrote:
: >Pierre.Bru@spotimage.fr wrote:
: > this algorithm is year-2000-proof, leap-year-proof, century-leap-year-proof
: > and you can compute julain day with this algorithm back to the begining of
: > the gregorian calendar, october 15, 1582.

: The best information I have here is the start date of the Gregorian Calendar!!!

Here's some more. Don't know where I got it from.

cu,
  Martin


   Brief History of the Gregorian Calendar
   ---------------------------------------

   As decreed by Pope Gregory XIII, October 4, 1582, was
   followed by October 15, 1582. Thus ended the 1600-year
   reign of the Julian calendar upon which the Gregorian
   calendar is based, and thus began the calendar which
   DECwindows Calendar uses to measure time.

   Calendars based on sun and moon movement were used even
   by the ancients, but the first reasonably accurate one was
   the 365<-day cycle calculated by the Greek Sosigenes. This
   was the calendar authorized by Julius Caesar in 46 B.C. The
   Julian calendar (not to be confused with the Julian period;
   see above) had 3 years of 365 days each, followed by a fourth
   year of 366 days.

     The 365<-day cycle was more accurately defined in 730 AD
   by the Venerable Bede, an Anglo-Saxon monk, who shortened
   the time by 11 minutes, 14 seconds. This accumulates to a
   whole day's error every 128 years, or a little more than 3
   days every 400 years. This being the Dark Ages, nothing was
   done to adjust the Calendar, despite Roger Bacon sending a
   note to Pope Clement IV, informing him of the drifting of the
   date for the vernal equinox. Later, Pope Sixtus IV did become
   convinced that another reform was needed and called the
   German astronomer Regiomontanus to Rome to advise him.
   Unfortunately, Regiomontanus died of the plague shortly
   thereafter and the plans died with him.

   Thursday, October 4, 1582 was the next time the calendar
   was adjusted.  This last day of the Julian calendar was
   followed by Friday, October 15. So began the Gregorian
   calendar that we use today, named after Pope Gregory XIII.
   He commissioned the mathematician Father Christopher
   Clavius, S.J., to do the necessary calculations, having been
   authorized to reform the calendar by the Council of Trent in
   1545.

   The Vatican librarian Aloysius Giglio provided a formula for
   long-range accuracy. He suggested that every fourth year
   be a leap year, except for century years that are not divisible
   by 400. Thus 1700, 1800, and 1900 would not be leap years,
   but 2000 would be, because 2000 is divisible by 400. This
   rule eliminated 3 leap years every 4 centuries, making the
   calendar sufficiently correct for most ordinary purposes.

   Political Acceptance World Wide
   -------------------------------

   The new calendar was adopted at once by France, Spain,
   Italy, Portugal, and Luxembourg. By 1584, Belgium, parts
   of the Netherlands, Switzerland, and most Catholic German
   states had joined, and by 1587, so had Hungary. It was not
   until 1699-1700 that these countries were joined by the rest
   of the Netherlands, Denmark, and the Protestant German
   countries.

   By the time the British imposed the calendar on all its
   possessions, in 1752, 11 days needed to be lost. September 2,
   1752, was thus decreed to be followed by September 14. In
   addition, New Year's day was moved back from March 25 to
   January 1. (For example, before, March 24, 1700 had been
   followed by March 25, 1701). Among other repercussions,
   this moved Washington's birth date from February 11, 1731,
   to February 22, 1732. The following year, 1753, Sweden too
   adopted the calendar.

   In 1793, the French Revolutionary government adopted a
   calendar of 12 months of 30 days each, with 5 extra days in
   September (6 on leap years). The Gregorian calendar was
   reinstated in 1806 by Napoleon.
 
   Adoption of the calendar in countries outside Europe and
   its Crown possessions occurred much later, and often in
   conjunction with political upheaval: Japan in 1873, Egypt in
   1875, China in 1912, and Turkey in 1917.

   In 1918, Russia's revolutionary government decreed that
   January 31, 1918, would be followed by February 14, 1918.
--
                          | Martin Vorlaender | VMS & WNT programmer
 OpenVMS: Where do you    | work: mv@pdv-systeme.de
 want to BE today?        |       http://www.pdv-systeme.de/users/martinv/
                          | home: martin@radiogaga.harz.de



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~1998-08-24  4:00 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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 ` Jim Becker
1998-08-17  4:00 ` Kevin Handy
1998-08-20  4:00 ` George Cornelius

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox