2 Modified BLP 8/28/95, 12/15/99 for PSPP.
4 Original sources for julcal.c and julcal.h can be found at
5 ftp.cdrom.com in /pub/algorithms/c/julcal10/{julcal.c,julcal.h}.
9 Translated from Pascal to C by Jim Van Zandt, July 1992.
11 Error-free translation based on error-free PL/I source
13 Based on Pascal code copyright 1985 by Michael A. Covington,
14 published in P.C. Tech Journal, December 1985, based on formulae
15 appearing in Astronomical Formulae for Calculators by Jean Meeus
18 /*#include <config.h>*/
23 #define JUL_OFFSET 2299160L
25 /* Takes Y, M, and D, and returns the corresponding Julian date as an
26 offset in days from the midnight separating 8 Oct 1582 and 9 Oct
27 1582. (Y,M,D) = (1999,10,1) corresponds to 1 Oct 1999. */
29 calendar_to_julian (int y, int m, int d)
35 assert (m > -12 && m < 12);
42 assert (m >= 0 && m < 12);
51 return ((1461 * (y + 4716L) / 4)
62 /* Takes a Julian date JD and sets *Y0, *M0, and *D0 to the
63 corresponding year, month, and day, respectively, where
64 (*Y0,*M0,*D0) = (1999,10,1) would be 1 Oct 1999. */
66 julian_to_calendar (long jd, int *y0, int *m0, int *d0)
76 ab = 31 * (aa / 1460969L);
78 ab += 3 * (aa / 146097L);
91 ay = (20 * b - 2442) / 7305;
94 em = 10000 * ee / 306001;
96 *d0 = ee - 306001L * em / 10000L;
99 if (y0 != NULL || m0 != NULL)
118 /* Takes a julian date JD and returns the corresponding year-relative
119 Julian date, with 1=Jan 1. */
121 julian_to_jday (long jd)
125 julian_to_calendar (jd, &year, NULL, NULL);
126 return jd - calendar_to_julian (year, 1, 1) + 1;
130 /* Takes a julian date JD and returns the corresponding weekday 1...7,
133 julian_to_wday (long jd)
135 return (jd - 3) % 7 + 1;
147 1, 50000, 102, 1157, 14288, 87365, 109623, 153211, 152371, 144623,
151 for (j = 0; j < sizeof julian / sizeof *julian; j++)
155 julian_to_calendar (julian[j], &y, &m, &d);
156 jd = calendar_to_julian (y, m, d);
157 printf ("%ld => %d/%d/%d => %ld\n", julian[j], y, m, d, jd);
164 {1582,10,15}, {1719,9,6}, {1583,1,24}, {1585,12,14},
165 {1621,11,26}, {1821,12,25}, {1882,12,3}, {2002,4,6},
166 {1999,12,19}, {1978,10,1},
170 for (j = 0; j < sizeof date / sizeof *date; j++)
172 int y = date[j][0], m = date[j][1], d = date[j][2];
173 long jd = calendar_to_julian (y, m, d);
175 julian_to_calendar (jd, &y2, &m2, &d2);
176 printf ("%d/%d/%d => %ld => %d/%d/%d\n",
177 y, m, d, jd, y2, m2, d2);