🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: BEST date <=> daynum converter?

#2256
From: Richard Wells
Date: Tue, 12 Mar 1996 02:49
48 lines
1833 bytes
Anthony Heading wrote:
 > 
 >   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.
 > 
 > 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;
 > }

Geez, I'd hate to be the maintenance programmer trying to
fix a bug in this!  Why not just use the routines in
time.h?

fill a tm structure with the proper values (wday and
    yday are not needed)
call mktime to get a time_t
call localtime to get a tm structure with wday and
    yday filled in
Technical Details
Message-ID: <4i2oo2$9l2@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4hv04d$j80@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Richard Wells <rdwells@mmm.com>
Newsgroups: comp.lang.c.moderated
Subject: Re: BEST date <=> daynum converter?
Date: 11 Mar 1996 20:49:37 -0600
Organization: 3M Company
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4i2oo2$9l2@solutions.solon.com>
References: <4hv04d$j80@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Mailer: Mozilla 2.0 (WinNT; I)