time.h design issues

D. J. Bernstein djb at cr.yp.to
Sat Aug 28 05:52:20 UTC 1999


Here are easy answers to Paul Hill's date-calculation challenges using
libtai's date functions. caldate_mjd() converts from a calendar date to
the traditional MJD number, and caldate_frommjd() converts back.

Paul Hill writes:
> the beginning next week,

day = caldate_mjd(&cd);
do
  caldate_frommjd(&cd,++day,&weekday,0);
while (weekday != 0);
/* there are faster ways to do this, of course */

> the first day of this year,

cd.month = 1;
cd.day = 1;

> start of next month,

++cd.month;
cd.day = 1;
caldate_normalize(&cd); /* same as caldate_frommjd(&cd,caldate_mjd(&cd),0,0); */

> two days before the beginning of the last month of the year,

cd.month = 12;
cd.day = -1;
caldate_normalize(&cd);

> the last Thursday in November,

cd.month = 12;
cd.day = 1;
day = caldate_mjd(&cd);
do
  caldate_frommjd(&cd,--day,&weekday,0);
while (weekday != 4);

> exactly 60 days from now,

caldate_frommjd(&cd,caldate_mjd(&cd) + 60,0,0);

> exactly two months before now,

In my experience, as illustrated previously on this mailing list, people
who use that phrase don't know what they're talking about. Try this:

d = cd.day;
cd.month -= 1;
cd.day = 0;
caldate_normalize(&cd);
if (cd.day > d)
  cd.day = d;

---Dan



More information about the tz mailing list