🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2219

BEST date <=> daynum converter?

#2219
From: Anthony Heading
Date: Sun, 10 Mar 1996 16:31
58 lines
1945 bytes
Hi,

  What's the speediest algorithm for conversion between day-month-year
dates and "days since 1janXX"?  I'd like to find something really fast
which only need cope with dates between say 1970 and 2100.

One rather nice thing I found was the method ascribed to Fliegel
and Van Flandern (CACM 1968); an (untested) implementation attached.
It avoids using floating point, which is nice.  Many other routines
seem to, but I always worry about doing essentially discrete maths with
floats.  Is that paranoid?  

But perhaps the lack of any conditionals below is excessive.  There must
be something faster/simpler.

Any thoughts?

Anthony


static int cal2days(date_t *d)
{
    register int i = d->year;
    register int j = d->month;
    register int k = d->day;
    register int l = (j - 14) / 12;
    return (d->dnum =
            k - 32075 + 1461 * (i + 4800 + l) / 4
            + 367 * (j - 2 - l * 12) / 12
            - 3 * ((i + 4900 + l) / 100) / 4
            );
}

static void days2cal(date_t *d)
{
   register int i, j, k, l, n;

   l = d->dnum + 68569;
   n = 4 * l / 146097;
   l -= (146097 * n + 3) / 4;
   i = 4000 * (l + 1) / 1461001;
   l += 31 - 1461 * i / 4;
   j = 80 * l / 2447;
   k = l - 2447 * j / 80;
   l = j / 11;
   j += 2 - 12 * l;
   i = 100 * (n - 49) + i + l;

   d->year = i;
   d->month = j;
   d->dnum = k;
}



-- 
Anthony J.R. Heading    J.P. Morgan Securities Ltd, London
Email: heading_anthony@jpmorgan.com  Tel: +44 171 325 5962
Technical Details
Message-ID: <4hv04d$j80@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: heading_anthony@jpmorgan.com (Anthony Heading)
Newsgroups: comp.lang.c.moderated
Subject: BEST date <=> daynum converter?
Date: 10 Mar 1996 10:31:09 -0600
Organization: J.P. Morgan Securities Ltd
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hv04d$j80@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Gnus v5.1