34static char rcsid[]not_used = {
39#include <libdap/Error.h>
48 days_arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
54 return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
59double days_in_year(
int year)
61 return is_leap(year) ? 366.0 : 365.0;
64static inline int days(
int year,
int month)
66 if (!(year > 0) || !(month > 0 && month < 13))
67 throw Error(malformed_expr,
"Date year or month is bad.");
69 if (month == 2 && is_leap(year))
72 return days_arr[month];
86long julian_day(
int year,
int month,
int day)
89 throw Error(malformed_expr,
"A date's year must be greater the zero.");
90 if (!(month > 0 && month < 13))
91 throw Error(malformed_expr,
92 "A date's month must be between zero and thirteen.");
94 if (!(day > 0 && day <= days(year, month)))
95 throw Error(malformed_expr,
96 "A date's day must be between zero and 28-31, depending on the month.");
100 jdn = (long) year * 367 + month * 275 / 9 - (year + (month > 2)) * 7 / 4
101 - ((year - (month < 3)) / 100 + 1) * 3 / 4 + day + 1721029L;
121void gregorian_date(
double jd,
int *year,
int *month,
int *day,
int *hours,
122 int *minutes,
double *seconds)
127 double tmp, frac = jd - j;
138 *year = (4L * j - 1L) / 146097L;
139 j = 4L * j - 1L - 146097L * *year;
141 j = (4L * *day + 3L) / 1461L;
142 *day = 4L * *day + 3L - 1461L * j;
143 *day = (*day + 4L) / 4L;
144 *month = (5L * *day - 3L) / 153L;
145 *day = 5L * *day - 3 - 153L * *month;
146 *day = (*day + 5L) / 5L;
147 *year = 100L * *year + j;
155 tmp = 3600.0 * (frac * 24.0);
156 *hours = (
int) (tmp / 3600.0);
157 tmp = tmp - *hours * 3600.0;
158 *minutes = (
int) (tmp / 60.0);
159 *seconds = tmp - *minutes * 60.0;
169int month_day_to_days(
int year,
int month,
int day)
172 throw Error(malformed_expr,
"A date's year must be greater the zero.");
173 if (!(month > 0 && month < 13))
174 throw Error(malformed_expr,
175 "A date's month must be between zero and thirteen.");
177 if (!(day > 0 && day <= days(year, month)))
178 throw Error(malformed_expr,
179 "A date's day must be between zero and 28-31, depending on the month.");
184 ddd += days(year, month);
191int days_in_month(
int year,
int month)
211 daysInMonth = (is_leap(year)) ? 29 : 28;
221 throw Error(
"Months must be numbered between 1 and 12 inclusive.");
238void days_to_month_day(
int year,
int ddd,
int *month,
int *day)
241 assert(ddd> 0 && ddd <= 365 + is_leap(year));
245 while (ddd > days(year, *month))
246 ddd -= days(year, (*month)++);
257int dayofweek(
double j)