2 * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
39 int year; /* 19xx, 20xx, 21xx */
40 int easter; /* Julian day */
41 int paskha; /* Julian day */
42 int cny; /* Julian day */
43 int firstdayofweek; /* 0 .. 6 */
44 struct cal_month *months;
45 struct cal_year *nextyear;
49 int month; /* 01 .. 12 */
50 int firstdayjulian; /* 000 .. 366 */
51 int firstdayofweek; /* 0 .. 6 */
52 struct cal_year *year; /* points back */
54 struct cal_month *nextmonth;
58 int dayofmonth; /* 01 .. 31 */
59 int julianday; /* 000 .. 366 */
60 int dayofweek; /* 0 .. 6 */
61 struct cal_day *nextday;
62 struct cal_month *month; /* points back */
63 struct cal_year *year; /* points back */
67 int debug_remember = 0;
68 static struct cal_year *hyear = NULL;
70 /* 1-based month, 0-based days, cumulative */
71 int cumdaytab[][14] = {
72 {0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
73 {0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
75 /* 1-based month, individual */
76 static int *monthdays;
77 int monthdaytab[][14] = {
78 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
79 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
82 static struct cal_day * find_day(int yy, int mm, int dd);
85 createdate(int y, int m, int d)
87 struct cal_year *py, *pyp;
88 struct cal_month *pm, *pmp;
89 struct cal_day *pd, *pdp;
95 if (py->year == y + 1900)
104 py = (struct cal_year *)calloc(1, sizeof(struct cal_year));
106 py->easter = easter(y);
107 py->paskha = paskha(y);
113 localtime_r(&t, &td);
114 py->firstdayofweek = td.tm_wday;
120 /* The very very very first one */
134 pm = (struct cal_month *)calloc(1, sizeof(struct cal_month));
137 cumday = cumdaytab[isleap(y)];
138 pm->firstdayjulian = cumday[m] + 2;
140 (py->firstdayofweek + pm->firstdayjulian -1) % 7;
154 if (pd == NULL) { /* Always true */
155 pd = (struct cal_day *)calloc(1, sizeof(struct cal_day));
159 pd->julianday = pm->firstdayjulian + d - 1;
160 pd->dayofweek = (pm->firstdayofweek + d - 1) % 7;
169 generatedates(struct tm *tp1, struct tm *tp2)
176 m1 = tp1->tm_mon + 1;
179 m2 = tp2->tm_mon + 1;
184 /* Same year, same month. Easy! */
185 for (d = d1; d <= d2; d++)
186 createdate(y1, m1, d);
190 * Same year, different month.
191 * - Take the leftover days from m1
192 * - Take all days from <m1 .. m2>
193 * - Take the first days from m2
195 monthdays = monthdaytab[isleap(y1)];
196 for (d = d1; d <= monthdays[m1]; d++)
197 createdate(y1, m1, d);
198 for (m = m1 + 1; m < m2; m++)
199 for (d = 1; d <= monthdays[m]; d++)
200 createdate(y1, m, d);
201 for (d = 1; d <= d2; d++)
202 createdate(y1, m2, d);
206 * Different year, different month.
207 * - Take the leftover days from y1-m1
208 * - Take all days from y1-<m1 .. 12]
209 * - Take all days from <y1 .. y2>
210 * - Take all days from y2-[1 .. m2>
211 * - Take the first days of y2-m2
213 monthdays = monthdaytab[isleap(y1)];
214 for (d = d1; d <= monthdays[m1]; d++)
215 createdate(y1, m1, d);
216 for (m = m1 + 1; m <= 12; m++)
217 for (d = 1; d <= monthdays[m]; d++)
218 createdate(y1, m, d);
219 for (y = y1 + 1; y < y2; y++) {
220 monthdays = monthdaytab[isleap(y)];
221 for (m = 1; m <= 12; m++)
222 for (d = 1; d <= monthdays[m]; d++)
225 monthdays = monthdaytab[isleap(y2)];
226 for (m = 1; m < m2; m++)
227 for (d = 1; d <= monthdays[m]; d++)
228 createdate(y2, m, d);
229 for (d = 1; d <= d2; d++)
230 createdate(y2, m2, d);
242 printf("%-5d (wday:%d)\n", y->year, y->firstdayofweek);
245 printf("-- %-5d (julian:%d, dow:%d)\n", m->month,
246 m->firstdayjulian, m->firstdayofweek);
249 printf(" -- %-5d (julian:%d, dow:%d)\n",
250 d->dayofmonth, d->julianday, d->dayofweek);
260 remember_ymd(int yy, int mm, int dd)
267 printf("remember_ymd: %d - %d - %d\n", yy, mm, dd);
277 if (m->month != mm) {
283 if (d->dayofmonth == dd)
296 remember_yd(int yy, int dd, int *rm, int *rd)
303 printf("remember_yd: %d - %d\n", yy, dd);
315 if (d->julianday == dd) {
330 first_dayofweek_of_year(int yy)
337 return (y->firstdayofweek);
341 /* Should not happen */
346 first_dayofweek_of_month(int yy, int mm)
360 return (m->firstdayofweek);
363 /* No data for this month */
367 /* No data for this year. Error? */
372 walkthrough_dates(struct event **e)
374 static struct cal_year *y = NULL;
375 static struct cal_month *m = NULL;
376 static struct cal_day *d = NULL;
385 if (d->nextday != NULL) {
390 if (m->nextmonth != NULL) {
396 if (y->nextyear != NULL) {
407 static struct cal_day *
408 find_day(int yy, int mm, int dd)
415 printf("remember_ymd: %d - %d - %d\n", yy, mm, dd);
425 if (m->month != mm) {
431 if (d->dayofmonth == dd)
444 addtodate(struct event *e, int year, int month, int day)
448 d = find_day(year, month, day);