2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
41 int year; /* 19xx, 20xx, 21xx */
42 int easter; /* Julian day */
43 int paskha; /* Julian day */
44 int cny; /* Julian day */
45 int firstdayofweek; /* 0 .. 6 */
46 struct cal_month *months;
47 struct cal_year *nextyear;
51 int month; /* 01 .. 12 */
52 int firstdayjulian; /* 000 .. 366 */
53 int firstdayofweek; /* 0 .. 6 */
54 struct cal_year *year; /* points back */
56 struct cal_month *nextmonth;
60 int dayofmonth; /* 01 .. 31 */
61 int julianday; /* 000 .. 366 */
62 int dayofweek; /* 0 .. 6 */
63 struct cal_day *nextday;
64 struct cal_month *month; /* points back */
65 struct cal_year *year; /* points back */
67 struct event *lastevent;
70 int debug_remember = 0;
71 static struct cal_year *hyear = NULL;
73 /* 1-based month, 0-based days, cumulative */
74 int cumdaytab[][14] = {
75 {0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
76 {0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
78 /* 1-based month, individual */
79 static int *monthdays;
80 int monthdaytab[][14] = {
81 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
82 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
85 static struct cal_day * find_day(int yy, int mm, int dd);
88 createdate(int y, int m, int d)
90 struct cal_year *py, *pyp;
91 struct cal_month *pm, *pmp;
92 struct cal_day *pd, *pdp;
98 if (py->year == y + 1900)
107 py = (struct cal_year *)calloc(1, sizeof(struct cal_year));
109 py->easter = easter(y);
110 py->paskha = paskha(y);
116 localtime_r(&t, &td);
117 py->firstdayofweek = td.tm_wday;
123 /* The very very very first one */
137 pm = (struct cal_month *)calloc(1, sizeof(struct cal_month));
140 cumday = cumdaytab[isleap(y)];
141 pm->firstdayjulian = cumday[m] + 2;
143 (py->firstdayofweek + pm->firstdayjulian -1) % 7;
157 if (pd == NULL) { /* Always true */
158 pd = (struct cal_day *)calloc(1, sizeof(struct cal_day));
162 pd->julianday = pm->firstdayjulian + d - 1;
163 pd->dayofweek = (pm->firstdayofweek + d - 1) % 7;
172 generatedates(struct tm *tp1, struct tm *tp2)
179 m1 = tp1->tm_mon + 1;
182 m2 = tp2->tm_mon + 1;
187 /* Same year, same month. Easy! */
188 for (d = d1; d <= d2; d++)
189 createdate(y1, m1, d);
193 * Same year, different month.
194 * - Take the leftover days from m1
195 * - Take all days from <m1 .. m2>
196 * - Take the first days from m2
198 monthdays = monthdaytab[isleap(y1)];
199 for (d = d1; d <= monthdays[m1]; d++)
200 createdate(y1, m1, d);
201 for (m = m1 + 1; m < m2; m++)
202 for (d = 1; d <= monthdays[m]; d++)
203 createdate(y1, m, d);
204 for (d = 1; d <= d2; d++)
205 createdate(y1, m2, d);
209 * Different year, different month.
210 * - Take the leftover days from y1-m1
211 * - Take all days from y1-<m1 .. 12]
212 * - Take all days from <y1 .. y2>
213 * - Take all days from y2-[1 .. m2>
214 * - Take the first days of y2-m2
216 monthdays = monthdaytab[isleap(y1)];
217 for (d = d1; d <= monthdays[m1]; d++)
218 createdate(y1, m1, d);
219 for (m = m1 + 1; m <= 12; m++)
220 for (d = 1; d <= monthdays[m]; d++)
221 createdate(y1, m, d);
222 for (y = y1 + 1; y < y2; y++) {
223 monthdays = monthdaytab[isleap(y)];
224 for (m = 1; m <= 12; m++)
225 for (d = 1; d <= monthdays[m]; d++)
228 monthdays = monthdaytab[isleap(y2)];
229 for (m = 1; m < m2; m++)
230 for (d = 1; d <= monthdays[m]; d++)
231 createdate(y2, m, d);
232 for (d = 1; d <= d2; d++)
233 createdate(y2, m2, d);
245 printf("%-5d (wday:%d)\n", y->year, y->firstdayofweek);
248 printf("-- %-5d (julian:%d, dow:%d)\n", m->month,
249 m->firstdayjulian, m->firstdayofweek);
252 printf(" -- %-5d (julian:%d, dow:%d)\n",
253 d->dayofmonth, d->julianday, d->dayofweek);
263 remember_ymd(int yy, int mm, int dd)
270 printf("remember_ymd: %d - %d - %d\n", yy, mm, dd);
280 if (m->month != mm) {
286 if (d->dayofmonth == dd)
299 remember_yd(int yy, int dd, int *rm, int *rd)
306 printf("remember_yd: %d - %d\n", yy, dd);
318 if (d->julianday == dd) {
333 first_dayofweek_of_year(int yy)
340 return (y->firstdayofweek);
344 /* Should not happen */
349 first_dayofweek_of_month(int yy, int mm)
363 return (m->firstdayofweek);
366 /* No data for this month */
370 /* No data for this year. Error? */
375 walkthrough_dates(struct event **e)
377 static struct cal_year *y = NULL;
378 static struct cal_month *m = NULL;
379 static struct cal_day *d = NULL;
388 if (d->nextday != NULL) {
393 if (m->nextmonth != NULL) {
399 if (y->nextyear != NULL) {
410 static struct cal_day *
411 find_day(int yy, int mm, int dd)
418 printf("remember_ymd: %d - %d - %d\n", yy, mm, dd);
428 if (m->month != mm) {
434 if (d->dayofmonth == dd)
447 addtodate(struct event *e)
452 d = find_day(e->year, e->month, e->day);