1fd1efedcSConrad Meyer /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3fd1efedcSConrad Meyer *
4fd1efedcSConrad Meyer * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
5fd1efedcSConrad Meyer * All rights reserved.
6fd1efedcSConrad Meyer *
7fd1efedcSConrad Meyer * Redistribution and use in source and binary forms, with or without
8fd1efedcSConrad Meyer * modification, are permitted provided that the following conditions
9fd1efedcSConrad Meyer * are met:
10fd1efedcSConrad Meyer * 1. Redistributions of source code must retain the above copyright
11fd1efedcSConrad Meyer * notice, this list of conditions and the following disclaimer.
12fd1efedcSConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright
13fd1efedcSConrad Meyer * notice, this list of conditions and the following disclaimer in the
14fd1efedcSConrad Meyer * documentation and/or other materials provided with the distribution.
15fd1efedcSConrad Meyer *
16fd1efedcSConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fd1efedcSConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fd1efedcSConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fd1efedcSConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20fd1efedcSConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fd1efedcSConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fd1efedcSConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fd1efedcSConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fd1efedcSConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fd1efedcSConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fd1efedcSConrad Meyer * SUCH DAMAGE.
27fd1efedcSConrad Meyer *
28fd1efedcSConrad Meyer */
29fd1efedcSConrad Meyer
30fd1efedcSConrad Meyer #include <sys/cdefs.h>
31fd1efedcSConrad Meyer #include <stdio.h>
32fd1efedcSConrad Meyer #include <stdlib.h>
33fd1efedcSConrad Meyer #include <err.h>
34fd1efedcSConrad Meyer #include <time.h>
35fd1efedcSConrad Meyer
36fd1efedcSConrad Meyer #include "calendar.h"
37fd1efedcSConrad Meyer
38fd1efedcSConrad Meyer struct cal_year {
39fd1efedcSConrad Meyer int year; /* 19xx, 20xx, 21xx */
40fd1efedcSConrad Meyer int easter; /* Julian day */
41fd1efedcSConrad Meyer int paskha; /* Julian day */
42fd1efedcSConrad Meyer int cny; /* Julian day */
43fd1efedcSConrad Meyer int firstdayofweek; /* 0 .. 6 */
44fd1efedcSConrad Meyer struct cal_month *months;
45fd1efedcSConrad Meyer struct cal_year *nextyear;
46fd1efedcSConrad Meyer };
47fd1efedcSConrad Meyer
48fd1efedcSConrad Meyer struct cal_month {
49fd1efedcSConrad Meyer int month; /* 01 .. 12 */
50fd1efedcSConrad Meyer int firstdayjulian; /* 000 .. 366 */
51fd1efedcSConrad Meyer int firstdayofweek; /* 0 .. 6 */
52fd1efedcSConrad Meyer struct cal_year *year; /* points back */
53fd1efedcSConrad Meyer struct cal_day *days;
54fd1efedcSConrad Meyer struct cal_month *nextmonth;
55fd1efedcSConrad Meyer };
56fd1efedcSConrad Meyer
57fd1efedcSConrad Meyer struct cal_day {
58fd1efedcSConrad Meyer int dayofmonth; /* 01 .. 31 */
59fd1efedcSConrad Meyer int julianday; /* 000 .. 366 */
60fd1efedcSConrad Meyer int dayofweek; /* 0 .. 6 */
61fd1efedcSConrad Meyer struct cal_day *nextday;
62fd1efedcSConrad Meyer struct cal_month *month; /* points back */
63fd1efedcSConrad Meyer struct cal_year *year; /* points back */
64fd1efedcSConrad Meyer struct event *events;
65ddad3a38SStefan Eßer struct event *lastevent;
66fd1efedcSConrad Meyer };
67fd1efedcSConrad Meyer
68fd1efedcSConrad Meyer int debug_remember = 0;
69fd1efedcSConrad Meyer static struct cal_year *hyear = NULL;
70fd1efedcSConrad Meyer
71fd1efedcSConrad Meyer /* 1-based month, 0-based days, cumulative */
72fd1efedcSConrad Meyer int cumdaytab[][14] = {
73fd1efedcSConrad Meyer {0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
74fd1efedcSConrad Meyer {0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
75fd1efedcSConrad Meyer };
76fd1efedcSConrad Meyer /* 1-based month, individual */
77fd1efedcSConrad Meyer static int *monthdays;
78fd1efedcSConrad Meyer int monthdaytab[][14] = {
79fd1efedcSConrad Meyer {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
80fd1efedcSConrad Meyer {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30},
81fd1efedcSConrad Meyer };
82fd1efedcSConrad Meyer
83fd1efedcSConrad Meyer static struct cal_day * find_day(int yy, int mm, int dd);
84fd1efedcSConrad Meyer
85fd1efedcSConrad Meyer static void
createdate(int y,int m,int d)86fd1efedcSConrad Meyer createdate(int y, int m, int d)
87fd1efedcSConrad Meyer {
88fd1efedcSConrad Meyer struct cal_year *py, *pyp;
89fd1efedcSConrad Meyer struct cal_month *pm, *pmp;
90fd1efedcSConrad Meyer struct cal_day *pd, *pdp;
91fd1efedcSConrad Meyer int *cumday;
92fd1efedcSConrad Meyer
93fd1efedcSConrad Meyer pyp = NULL;
94fd1efedcSConrad Meyer py = hyear;
95fd1efedcSConrad Meyer while (py != NULL) {
96fd1efedcSConrad Meyer if (py->year == y + 1900)
97fd1efedcSConrad Meyer break;
98fd1efedcSConrad Meyer pyp = py;
99fd1efedcSConrad Meyer py = py->nextyear;
100fd1efedcSConrad Meyer }
101fd1efedcSConrad Meyer
102fd1efedcSConrad Meyer if (py == NULL) {
103fd1efedcSConrad Meyer struct tm td;
104fd1efedcSConrad Meyer time_t t;
105fd1efedcSConrad Meyer py = (struct cal_year *)calloc(1, sizeof(struct cal_year));
106fd1efedcSConrad Meyer py->year = y + 1900;
107fd1efedcSConrad Meyer py->easter = easter(y);
108fd1efedcSConrad Meyer py->paskha = paskha(y);
109fd1efedcSConrad Meyer
110fd1efedcSConrad Meyer td = tm0;
111fd1efedcSConrad Meyer td.tm_year = y;
112fd1efedcSConrad Meyer td.tm_mday = 1;
113fd1efedcSConrad Meyer t = mktime(&td);
114fd1efedcSConrad Meyer localtime_r(&t, &td);
115fd1efedcSConrad Meyer py->firstdayofweek = td.tm_wday;
116fd1efedcSConrad Meyer
117fd1efedcSConrad Meyer if (pyp != NULL)
118fd1efedcSConrad Meyer pyp->nextyear = py;
119fd1efedcSConrad Meyer }
120fd1efedcSConrad Meyer if (pyp == NULL) {
121fd1efedcSConrad Meyer /* The very very very first one */
122fd1efedcSConrad Meyer hyear = py;
123fd1efedcSConrad Meyer }
124fd1efedcSConrad Meyer
125fd1efedcSConrad Meyer pmp = NULL;
126fd1efedcSConrad Meyer pm = py->months;
127fd1efedcSConrad Meyer while (pm != NULL) {
128fd1efedcSConrad Meyer if (pm->month == m)
129fd1efedcSConrad Meyer break;
130fd1efedcSConrad Meyer pmp = pm;
131fd1efedcSConrad Meyer pm = pm->nextmonth;
132fd1efedcSConrad Meyer }
133fd1efedcSConrad Meyer
134fd1efedcSConrad Meyer if (pm == NULL) {
135fd1efedcSConrad Meyer pm = (struct cal_month *)calloc(1, sizeof(struct cal_month));
136fd1efedcSConrad Meyer pm->year = py;
137fd1efedcSConrad Meyer pm->month = m;
138fd1efedcSConrad Meyer cumday = cumdaytab[isleap(y)];
139fd1efedcSConrad Meyer pm->firstdayjulian = cumday[m] + 2;
140fd1efedcSConrad Meyer pm->firstdayofweek =
141fd1efedcSConrad Meyer (py->firstdayofweek + pm->firstdayjulian -1) % 7;
142fd1efedcSConrad Meyer if (pmp != NULL)
143fd1efedcSConrad Meyer pmp->nextmonth = pm;
144fd1efedcSConrad Meyer }
145fd1efedcSConrad Meyer if (pmp == NULL)
146fd1efedcSConrad Meyer py->months = pm;
147fd1efedcSConrad Meyer
148fd1efedcSConrad Meyer pdp = NULL;
149fd1efedcSConrad Meyer pd = pm->days;
150fd1efedcSConrad Meyer while (pd != NULL) {
151fd1efedcSConrad Meyer pdp = pd;
152fd1efedcSConrad Meyer pd = pd->nextday;
153fd1efedcSConrad Meyer }
154fd1efedcSConrad Meyer
155fd1efedcSConrad Meyer if (pd == NULL) { /* Always true */
156fd1efedcSConrad Meyer pd = (struct cal_day *)calloc(1, sizeof(struct cal_day));
157fd1efedcSConrad Meyer pd->month = pm;
158fd1efedcSConrad Meyer pd->year = py;
159fd1efedcSConrad Meyer pd->dayofmonth = d;
160fd1efedcSConrad Meyer pd->julianday = pm->firstdayjulian + d - 1;
161fd1efedcSConrad Meyer pd->dayofweek = (pm->firstdayofweek + d - 1) % 7;
162fd1efedcSConrad Meyer if (pdp != NULL)
163fd1efedcSConrad Meyer pdp->nextday = pd;
164fd1efedcSConrad Meyer }
165fd1efedcSConrad Meyer if (pdp == NULL)
166fd1efedcSConrad Meyer pm->days = pd;
167fd1efedcSConrad Meyer }
168fd1efedcSConrad Meyer
169fd1efedcSConrad Meyer void
generatedates(struct tm * tp1,struct tm * tp2)170fd1efedcSConrad Meyer generatedates(struct tm *tp1, struct tm *tp2)
171fd1efedcSConrad Meyer {
172fd1efedcSConrad Meyer int y1, m1, d1;
173fd1efedcSConrad Meyer int y2, m2, d2;
174fd1efedcSConrad Meyer int y, m, d;
175fd1efedcSConrad Meyer
176fd1efedcSConrad Meyer y1 = tp1->tm_year;
177fd1efedcSConrad Meyer m1 = tp1->tm_mon + 1;
178fd1efedcSConrad Meyer d1 = tp1->tm_mday;
179fd1efedcSConrad Meyer y2 = tp2->tm_year;
180fd1efedcSConrad Meyer m2 = tp2->tm_mon + 1;
181fd1efedcSConrad Meyer d2 = tp2->tm_mday;
182fd1efedcSConrad Meyer
183fd1efedcSConrad Meyer if (y1 == y2) {
184fd1efedcSConrad Meyer if (m1 == m2) {
185fd1efedcSConrad Meyer /* Same year, same month. Easy! */
186fd1efedcSConrad Meyer for (d = d1; d <= d2; d++)
187fd1efedcSConrad Meyer createdate(y1, m1, d);
188fd1efedcSConrad Meyer return;
189fd1efedcSConrad Meyer }
190fd1efedcSConrad Meyer /*
191fd1efedcSConrad Meyer * Same year, different month.
192fd1efedcSConrad Meyer * - Take the leftover days from m1
193fd1efedcSConrad Meyer * - Take all days from <m1 .. m2>
194fd1efedcSConrad Meyer * - Take the first days from m2
195fd1efedcSConrad Meyer */
196fd1efedcSConrad Meyer monthdays = monthdaytab[isleap(y1)];
197fd1efedcSConrad Meyer for (d = d1; d <= monthdays[m1]; d++)
198fd1efedcSConrad Meyer createdate(y1, m1, d);
199fd1efedcSConrad Meyer for (m = m1 + 1; m < m2; m++)
200fd1efedcSConrad Meyer for (d = 1; d <= monthdays[m]; d++)
201fd1efedcSConrad Meyer createdate(y1, m, d);
202fd1efedcSConrad Meyer for (d = 1; d <= d2; d++)
203fd1efedcSConrad Meyer createdate(y1, m2, d);
204fd1efedcSConrad Meyer return;
205fd1efedcSConrad Meyer }
206fd1efedcSConrad Meyer /*
207fd1efedcSConrad Meyer * Different year, different month.
208fd1efedcSConrad Meyer * - Take the leftover days from y1-m1
209fd1efedcSConrad Meyer * - Take all days from y1-<m1 .. 12]
210fd1efedcSConrad Meyer * - Take all days from <y1 .. y2>
211fd1efedcSConrad Meyer * - Take all days from y2-[1 .. m2>
212fd1efedcSConrad Meyer * - Take the first days of y2-m2
213fd1efedcSConrad Meyer */
214fd1efedcSConrad Meyer monthdays = monthdaytab[isleap(y1)];
215fd1efedcSConrad Meyer for (d = d1; d <= monthdays[m1]; d++)
216fd1efedcSConrad Meyer createdate(y1, m1, d);
217fd1efedcSConrad Meyer for (m = m1 + 1; m <= 12; m++)
218fd1efedcSConrad Meyer for (d = 1; d <= monthdays[m]; d++)
219fd1efedcSConrad Meyer createdate(y1, m, d);
220fd1efedcSConrad Meyer for (y = y1 + 1; y < y2; y++) {
221fd1efedcSConrad Meyer monthdays = monthdaytab[isleap(y)];
222fd1efedcSConrad Meyer for (m = 1; m <= 12; m++)
223fd1efedcSConrad Meyer for (d = 1; d <= monthdays[m]; d++)
224fd1efedcSConrad Meyer createdate(y, m, d);
225fd1efedcSConrad Meyer }
226fd1efedcSConrad Meyer monthdays = monthdaytab[isleap(y2)];
227fd1efedcSConrad Meyer for (m = 1; m < m2; m++)
228fd1efedcSConrad Meyer for (d = 1; d <= monthdays[m]; d++)
229fd1efedcSConrad Meyer createdate(y2, m, d);
230fd1efedcSConrad Meyer for (d = 1; d <= d2; d++)
231fd1efedcSConrad Meyer createdate(y2, m2, d);
232fd1efedcSConrad Meyer }
233fd1efedcSConrad Meyer
234fd1efedcSConrad Meyer void
dumpdates(void)235fd1efedcSConrad Meyer dumpdates(void)
236fd1efedcSConrad Meyer {
237fd1efedcSConrad Meyer struct cal_year *y;
238fd1efedcSConrad Meyer struct cal_month *m;
239fd1efedcSConrad Meyer struct cal_day *d;
240fd1efedcSConrad Meyer
241fd1efedcSConrad Meyer y = hyear;
242fd1efedcSConrad Meyer while (y != NULL) {
243fd1efedcSConrad Meyer printf("%-5d (wday:%d)\n", y->year, y->firstdayofweek);
244fd1efedcSConrad Meyer m = y->months;
245fd1efedcSConrad Meyer while (m != NULL) {
246fd1efedcSConrad Meyer printf("-- %-5d (julian:%d, dow:%d)\n", m->month,
247fd1efedcSConrad Meyer m->firstdayjulian, m->firstdayofweek);
248fd1efedcSConrad Meyer d = m->days;
249fd1efedcSConrad Meyer while (d != NULL) {
250fd1efedcSConrad Meyer printf(" -- %-5d (julian:%d, dow:%d)\n",
251fd1efedcSConrad Meyer d->dayofmonth, d->julianday, d->dayofweek);
252fd1efedcSConrad Meyer d = d->nextday;
253fd1efedcSConrad Meyer }
254fd1efedcSConrad Meyer m = m->nextmonth;
255fd1efedcSConrad Meyer }
256fd1efedcSConrad Meyer y = y->nextyear;
257fd1efedcSConrad Meyer }
258fd1efedcSConrad Meyer }
259fd1efedcSConrad Meyer
260fd1efedcSConrad Meyer int
remember_ymd(int yy,int mm,int dd)261fd1efedcSConrad Meyer remember_ymd(int yy, int mm, int dd)
262fd1efedcSConrad Meyer {
263fd1efedcSConrad Meyer struct cal_year *y;
264fd1efedcSConrad Meyer struct cal_month *m;
265fd1efedcSConrad Meyer struct cal_day *d;
266fd1efedcSConrad Meyer
267fd1efedcSConrad Meyer if (debug_remember)
268fd1efedcSConrad Meyer printf("remember_ymd: %d - %d - %d\n", yy, mm, dd);
269fd1efedcSConrad Meyer
270fd1efedcSConrad Meyer y = hyear;
271fd1efedcSConrad Meyer while (y != NULL) {
272fd1efedcSConrad Meyer if (y->year != yy) {
273fd1efedcSConrad Meyer y = y->nextyear;
274fd1efedcSConrad Meyer continue;
275fd1efedcSConrad Meyer }
276fd1efedcSConrad Meyer m = y->months;
277fd1efedcSConrad Meyer while (m != NULL) {
278fd1efedcSConrad Meyer if (m->month != mm) {
279fd1efedcSConrad Meyer m = m->nextmonth;
280fd1efedcSConrad Meyer continue;
281fd1efedcSConrad Meyer }
282fd1efedcSConrad Meyer d = m->days;
283fd1efedcSConrad Meyer while (d != NULL) {
284fd1efedcSConrad Meyer if (d->dayofmonth == dd)
285fd1efedcSConrad Meyer return (1);
286fd1efedcSConrad Meyer d = d->nextday;
287fd1efedcSConrad Meyer continue;
288fd1efedcSConrad Meyer }
289fd1efedcSConrad Meyer return (0);
290fd1efedcSConrad Meyer }
291fd1efedcSConrad Meyer return (0);
292fd1efedcSConrad Meyer }
293fd1efedcSConrad Meyer return (0);
294fd1efedcSConrad Meyer }
295fd1efedcSConrad Meyer
296fd1efedcSConrad Meyer int
remember_yd(int yy,int dd,int * rm,int * rd)297fd1efedcSConrad Meyer remember_yd(int yy, int dd, int *rm, int *rd)
298fd1efedcSConrad Meyer {
299fd1efedcSConrad Meyer struct cal_year *y;
300fd1efedcSConrad Meyer struct cal_month *m;
301fd1efedcSConrad Meyer struct cal_day *d;
302fd1efedcSConrad Meyer
303fd1efedcSConrad Meyer if (debug_remember)
304fd1efedcSConrad Meyer printf("remember_yd: %d - %d\n", yy, dd);
305fd1efedcSConrad Meyer
306fd1efedcSConrad Meyer y = hyear;
307fd1efedcSConrad Meyer while (y != NULL) {
308fd1efedcSConrad Meyer if (y->year != yy) {
309fd1efedcSConrad Meyer y = y->nextyear;
310fd1efedcSConrad Meyer continue;
311fd1efedcSConrad Meyer }
312fd1efedcSConrad Meyer m = y->months;
313fd1efedcSConrad Meyer while (m != NULL) {
314fd1efedcSConrad Meyer d = m->days;
315fd1efedcSConrad Meyer while (d != NULL) {
316fd1efedcSConrad Meyer if (d->julianday == dd) {
317fd1efedcSConrad Meyer *rm = m->month;
318fd1efedcSConrad Meyer *rd = d->dayofmonth;
319fd1efedcSConrad Meyer return (1);
320fd1efedcSConrad Meyer }
321fd1efedcSConrad Meyer d = d->nextday;
322fd1efedcSConrad Meyer }
323fd1efedcSConrad Meyer m = m->nextmonth;
324fd1efedcSConrad Meyer }
325fd1efedcSConrad Meyer return (0);
326fd1efedcSConrad Meyer }
327fd1efedcSConrad Meyer return (0);
328fd1efedcSConrad Meyer }
329fd1efedcSConrad Meyer
330fd1efedcSConrad Meyer int
first_dayofweek_of_year(int yy)331fd1efedcSConrad Meyer first_dayofweek_of_year(int yy)
332fd1efedcSConrad Meyer {
333fd1efedcSConrad Meyer struct cal_year *y;
334fd1efedcSConrad Meyer
335fd1efedcSConrad Meyer y = hyear;
336fd1efedcSConrad Meyer while (y != NULL) {
337fd1efedcSConrad Meyer if (y->year == yy)
338fd1efedcSConrad Meyer return (y->firstdayofweek);
339fd1efedcSConrad Meyer y = y->nextyear;
340fd1efedcSConrad Meyer }
341fd1efedcSConrad Meyer
342fd1efedcSConrad Meyer /* Should not happen */
343fd1efedcSConrad Meyer return (-1);
344fd1efedcSConrad Meyer }
345fd1efedcSConrad Meyer
346fd1efedcSConrad Meyer int
first_dayofweek_of_month(int yy,int mm)347fd1efedcSConrad Meyer first_dayofweek_of_month(int yy, int mm)
348fd1efedcSConrad Meyer {
349fd1efedcSConrad Meyer struct cal_year *y;
350fd1efedcSConrad Meyer struct cal_month *m;
351fd1efedcSConrad Meyer
352fd1efedcSConrad Meyer y = hyear;
353fd1efedcSConrad Meyer while (y != NULL) {
354fd1efedcSConrad Meyer if (y->year != yy) {
355fd1efedcSConrad Meyer y = y->nextyear;
356fd1efedcSConrad Meyer continue;
357fd1efedcSConrad Meyer }
358fd1efedcSConrad Meyer m = y->months;
359fd1efedcSConrad Meyer while (m != NULL) {
360fd1efedcSConrad Meyer if (m->month == mm)
361fd1efedcSConrad Meyer return (m->firstdayofweek);
362fd1efedcSConrad Meyer m = m->nextmonth;
363fd1efedcSConrad Meyer }
364fd1efedcSConrad Meyer /* No data for this month */
365fd1efedcSConrad Meyer return (-1);
366fd1efedcSConrad Meyer }
367fd1efedcSConrad Meyer
368fd1efedcSConrad Meyer /* No data for this year. Error? */
369fd1efedcSConrad Meyer return (-1);
370fd1efedcSConrad Meyer }
371fd1efedcSConrad Meyer
372fd1efedcSConrad Meyer int
walkthrough_dates(struct event ** e)373fd1efedcSConrad Meyer walkthrough_dates(struct event **e)
374fd1efedcSConrad Meyer {
375fd1efedcSConrad Meyer static struct cal_year *y = NULL;
376fd1efedcSConrad Meyer static struct cal_month *m = NULL;
377fd1efedcSConrad Meyer static struct cal_day *d = NULL;
378fd1efedcSConrad Meyer
379fd1efedcSConrad Meyer if (y == NULL) {
380fd1efedcSConrad Meyer y = hyear;
381fd1efedcSConrad Meyer m = y->months;
382fd1efedcSConrad Meyer d = m->days;
383fd1efedcSConrad Meyer *e = d->events;
384fd1efedcSConrad Meyer return (1);
385fd1efedcSConrad Meyer }
386fd1efedcSConrad Meyer if (d->nextday != NULL) {
387fd1efedcSConrad Meyer d = d->nextday;
388fd1efedcSConrad Meyer *e = d->events;
389fd1efedcSConrad Meyer return (1);
390fd1efedcSConrad Meyer }
391fd1efedcSConrad Meyer if (m->nextmonth != NULL) {
392fd1efedcSConrad Meyer m = m->nextmonth;
393fd1efedcSConrad Meyer d = m->days;
394fd1efedcSConrad Meyer *e = d->events;
395fd1efedcSConrad Meyer return (1);
396fd1efedcSConrad Meyer }
397fd1efedcSConrad Meyer if (y->nextyear != NULL) {
398fd1efedcSConrad Meyer y = y->nextyear;
399fd1efedcSConrad Meyer m = y->months;
400fd1efedcSConrad Meyer d = m->days;
401fd1efedcSConrad Meyer *e = d->events;
402fd1efedcSConrad Meyer return (1);
403fd1efedcSConrad Meyer }
404fd1efedcSConrad Meyer
405fd1efedcSConrad Meyer return (0);
406fd1efedcSConrad Meyer }
407fd1efedcSConrad Meyer
408fd1efedcSConrad Meyer static struct cal_day *
find_day(int yy,int mm,int dd)409fd1efedcSConrad Meyer find_day(int yy, int mm, int dd)
410fd1efedcSConrad Meyer {
411fd1efedcSConrad Meyer struct cal_year *y;
412fd1efedcSConrad Meyer struct cal_month *m;
413fd1efedcSConrad Meyer struct cal_day *d;
414fd1efedcSConrad Meyer
415fd1efedcSConrad Meyer if (debug_remember)
416fd1efedcSConrad Meyer printf("remember_ymd: %d - %d - %d\n", yy, mm, dd);
417fd1efedcSConrad Meyer
418fd1efedcSConrad Meyer y = hyear;
419fd1efedcSConrad Meyer while (y != NULL) {
420fd1efedcSConrad Meyer if (y->year != yy) {
421fd1efedcSConrad Meyer y = y->nextyear;
422fd1efedcSConrad Meyer continue;
423fd1efedcSConrad Meyer }
424fd1efedcSConrad Meyer m = y->months;
425fd1efedcSConrad Meyer while (m != NULL) {
426fd1efedcSConrad Meyer if (m->month != mm) {
427fd1efedcSConrad Meyer m = m->nextmonth;
428fd1efedcSConrad Meyer continue;
429fd1efedcSConrad Meyer }
430fd1efedcSConrad Meyer d = m->days;
431fd1efedcSConrad Meyer while (d != NULL) {
432fd1efedcSConrad Meyer if (d->dayofmonth == dd)
433fd1efedcSConrad Meyer return (d);
434fd1efedcSConrad Meyer d = d->nextday;
435fd1efedcSConrad Meyer continue;
436fd1efedcSConrad Meyer }
437fd1efedcSConrad Meyer return (NULL);
438fd1efedcSConrad Meyer }
439fd1efedcSConrad Meyer return (NULL);
440fd1efedcSConrad Meyer }
441fd1efedcSConrad Meyer return (NULL);
442fd1efedcSConrad Meyer }
443fd1efedcSConrad Meyer
444fd1efedcSConrad Meyer void
addtodate(struct event * e)4452ceb17a8SStefan Eßer addtodate(struct event *e)
446fd1efedcSConrad Meyer {
447fd1efedcSConrad Meyer struct cal_day *d;
448ddad3a38SStefan Eßer struct event *ee;
449fd1efedcSConrad Meyer
4502ceb17a8SStefan Eßer d = find_day(e->year, e->month, e->day);
451ddad3a38SStefan Eßer ee = d->lastevent;
452ddad3a38SStefan Eßer if (ee != NULL)
453ddad3a38SStefan Eßer ee->next = e;
454ddad3a38SStefan Eßer else
455fd1efedcSConrad Meyer d->events = e;
456ddad3a38SStefan Eßer d->lastevent = e;
457fd1efedcSConrad Meyer }
458