xref: /freebsd/usr.bin/calendar/parsedata.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <ctype.h>
32 #include <math.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <err.h>
37 
38 #include "calendar.h"
39 
40 static char *showflags(int flags);
41 static int isonlydigits(char *s, int nostar);
42 static const char *getmonthname(int i);
43 static int checkmonth(char *s, size_t *len, size_t *offset, const char **month);
44 static const char *getdayofweekname(int i);
45 static int checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow);
46 static int indextooffset(char *s);
47 static int parseoffset(char *s);
48 static char *floattoday(int year, double f);
49 static char *floattotime(double f);
50 static int wdayom (int day, int offset, int month, int year);
51 
52 /*
53  * Expected styles:
54  *
55  * Date			::=	Month . ' ' . DayOfMonth |
56  *				Month . ' ' . DayOfWeek . ModifierIndex |
57  *				Month . '/' . DayOfMonth |
58  *				Month . '/' . DayOfWeek . ModifierIndex |
59  *				DayOfMonth . ' ' . Month |
60  *				DayOfMonth . '/' . Month |
61  *				DayOfWeek . ModifierIndex . ' ' .Month |
62  *				DayOfWeek . ModifierIndex . '/' .Month |
63  *				DayOfWeek . ModifierIndex |
64  *				SpecialDay . ModifierOffset
65  *
66  * Month		::=	MonthName | MonthNumber | '*'
67  * MonthNumber		::=	'0' ... '9' | '00' ... '09' | '10' ... '12'
68  * MonthName		::=	MonthNameShort | MonthNameLong
69  * MonthNameLong	::=	'January' ... 'December'
70  * MonthNameShort	::=	'Jan' ... 'Dec' | 'Jan.' ... 'Dec.'
71  *
72  * DayOfWeek		::=	DayOfWeekShort | DayOfWeekLong
73  * DayOfWeekShort	::=	'Mon' .. 'Sun'
74  * DayOfWeekLong	::=	'Monday' .. 'Sunday'
75  * DayOfMonth		::=	'0' ... '9' | '00' ... '09' | '10' ... '29' |
76  *				'30' ... '31' | '*'
77  *
78  * ModifierOffset	::=	'' | '+' . ModifierNumber | '-' . ModifierNumber
79  * ModifierNumber	::=	'0' ... '9' | '00' ... '99' | '000' ... '299' |
80  *				'300' ... '359' | '360' ... '365'
81  * ModifierIndex	::=	'Second' | 'Third' | 'Fourth' | 'Fifth' |
82  *				'First' | 'Last'
83  *
84  * SpecialDay		::=	'Easter' | 'Paskha' | 'ChineseNewYear'
85  *
86  */
87 static int
88 determinestyle(char *date, int *flags,
89     char *month, int *imonth, char *dayofmonth, int *idayofmonth,
90     char *dayofweek, int *idayofweek, char *modifieroffset,
91     char *modifierindex, char *specialday, char *year, int *iyear)
92 {
93 	char *p, *p1, *p2, *py;
94 	const char *dow, *pmonth;
95 	char pold;
96 	size_t len, offset;
97 
98 	*flags = F_NONE;
99 	*month = '\0';
100 	*imonth = 0;
101 	*year = '\0';
102 	*iyear = 0;
103 	*dayofmonth = '\0';
104 	*idayofmonth = 0;
105 	*dayofweek = '\0';
106 	*idayofweek = 0;
107 	*modifieroffset = '\0';
108 	*modifierindex = '\0';
109 	*specialday = '\0';
110 
111 #define CHECKSPECIAL(s1, s2, lens2, type)				\
112 	if (s2 != NULL && strncmp(s1, s2, lens2) == 0) {		\
113 		*flags |= F_SPECIALDAY;					\
114 		*flags |= type;						\
115 		*flags |= F_VARIABLE;					\
116 		if (strlen(s1) == lens2) {				\
117 			strcpy(specialday, s1);				\
118 			return (1);					\
119 		}							\
120 		strncpy(specialday, s1, lens2);				\
121 		specialday[lens2] = '\0';				\
122 		strcpy(modifieroffset, s1 + lens2);			\
123 		*flags |= F_MODIFIEROFFSET;				\
124 		return (1);						\
125 	}
126 
127 	if ((p = strchr(date, ' ')) == NULL) {
128 		if ((p = strchr(date, '/')) == NULL) {
129 			CHECKSPECIAL(date, STRING_CNY, strlen(STRING_CNY),
130 			    F_CNY);
131 			CHECKSPECIAL(date, ncny.name, ncny.len, F_CNY);
132 			CHECKSPECIAL(date, STRING_NEWMOON,
133 			    strlen(STRING_NEWMOON), F_NEWMOON);
134 			CHECKSPECIAL(date, nnewmoon.name, nnewmoon.len,
135 			    F_NEWMOON);
136 			CHECKSPECIAL(date, STRING_FULLMOON,
137 			    strlen(STRING_FULLMOON), F_FULLMOON);
138 			CHECKSPECIAL(date, nfullmoon.name, nfullmoon.len,
139 			    F_FULLMOON);
140 			CHECKSPECIAL(date, STRING_PASKHA,
141 			    strlen(STRING_PASKHA), F_PASKHA);
142 			CHECKSPECIAL(date, npaskha.name, npaskha.len, F_PASKHA);
143 			CHECKSPECIAL(date, STRING_EASTER,
144 			    strlen(STRING_EASTER), F_EASTER);
145 			CHECKSPECIAL(date, neaster.name, neaster.len, F_EASTER);
146 			CHECKSPECIAL(date, STRING_MAREQUINOX,
147 			    strlen(STRING_MAREQUINOX), F_MAREQUINOX);
148 			CHECKSPECIAL(date, nmarequinox.name, nmarequinox.len,
149 			    F_SEPEQUINOX);
150 			CHECKSPECIAL(date, STRING_SEPEQUINOX,
151 			    strlen(STRING_SEPEQUINOX), F_SEPEQUINOX);
152 			CHECKSPECIAL(date, nsepequinox.name, nsepequinox.len,
153 			    F_SEPEQUINOX);
154 			CHECKSPECIAL(date, STRING_JUNSOLSTICE,
155 			    strlen(STRING_JUNSOLSTICE), F_JUNSOLSTICE);
156 			CHECKSPECIAL(date, njunsolstice.name, njunsolstice.len,
157 			    F_JUNSOLSTICE);
158 			CHECKSPECIAL(date, STRING_DECSOLSTICE,
159 			    strlen(STRING_DECSOLSTICE), F_DECSOLSTICE);
160 			CHECKSPECIAL(date, ndecsolstice.name, ndecsolstice.len,
161 			    F_DECSOLSTICE);
162 			if (checkdayofweek(date, &len, &offset, &dow) != 0) {
163 				*flags |= F_DAYOFWEEK;
164 				*flags |= F_VARIABLE;
165 				*idayofweek = offset;
166 				if (strlen(date) == len) {
167 					strcpy(dayofweek, date);
168 					return (1);
169 				}
170 				strncpy(dayofweek, date, len);
171 				dayofweek[len] = '\0';
172 				strcpy(modifierindex, date + len);
173 				*flags |= F_MODIFIERINDEX;
174 				return (1);
175 			}
176 			if (isonlydigits(date, 1)) {
177 				/* Assume month number only */
178 				*flags |= F_MONTH;
179 				*imonth = (int)strtol(date, (char **)NULL, 10);
180 				strcpy(month, getmonthname(*imonth));
181 				return(1);
182 			}
183 			return (0);
184 		}
185 	}
186 
187 	/*
188 	 * After this, leave by goto-ing to "allfine" or "fail" to restore the
189 	 * original data in `date'.
190 	 */
191 	pold = *p;
192 	*p = 0;
193 	p1 = date;
194 	p2 = p + 1;
195 	/* Now p2 points to the next field and p1 to the first field */
196 
197 	if ((py = strchr(p2, '/')) != NULL) {
198 		/* We have a year in the string. Now this is getting tricky */
199 		strcpy(year, p1);
200 		*iyear = (int)strtol(year, NULL, 10);
201 		p1 = p2;
202 		p2 = py + 1;
203 		*py = 0;
204 		*flags |= F_YEAR;
205 	}
206 
207 	/* Check if there is a month-string in the date */
208 	if ((checkmonth(p1, &len, &offset, &pmonth) != 0)
209 	    || (checkmonth(p2, &len, &offset, &pmonth) != 0 && (p2 = p1))) {
210 		/* p2 is the non-month part */
211 		*flags |= F_MONTH;
212 		*imonth = offset;
213 
214 		strcpy(month, getmonthname(offset));
215 		if (isonlydigits(p2, 1)) {
216 			strcpy(dayofmonth, p2);
217 			*idayofmonth = (int)strtol(p2, (char **)NULL, 10);
218 			*flags |= F_DAYOFMONTH;
219 			goto allfine;
220 		}
221 		if (strcmp(p2, "*") == 0) {
222 			*flags |= F_ALLDAY;
223 			goto allfine;
224 		}
225 
226 		if (checkdayofweek(p2, &len, &offset, &dow) != 0) {
227 			*flags |= F_DAYOFWEEK;
228 			*flags |= F_VARIABLE;
229 			*idayofweek = offset;
230 			strcpy(dayofweek, getdayofweekname(offset));
231 			if (strlen(p2) == len)
232 				goto allfine;
233 			strcpy(modifierindex, p2 + len);
234 			*flags |= F_MODIFIERINDEX;
235 			goto allfine;
236 		}
237 		goto fail;
238 	}
239 
240 	/* Check if there is an every-day or every-month in the string */
241 	if ((strcmp(p1, "*") == 0 && isonlydigits(p2, 1))
242 	    || (strcmp(p2, "*") == 0 && isonlydigits(p1, 1) && (p2 = p1))) {
243 		int d;
244 
245 		*flags |= F_ALLMONTH;
246 		*flags |= F_DAYOFMONTH;
247 		d = (int)strtol(p2, (char **)NULL, 10);
248 		*idayofmonth = d;
249 		sprintf(dayofmonth, "%d", d);
250 		goto allfine;
251 	}
252 
253 	/* Month as a number, then a weekday */
254 	if (isonlydigits(p1, 1)
255 	    && checkdayofweek(p2, &len, &offset, &dow) != 0) {
256 		int d;
257 
258 		*flags |= F_MONTH;
259 		*flags |= F_DAYOFWEEK;
260 		*flags |= F_VARIABLE;
261 
262 		*idayofweek = offset;
263 		d = (int)strtol(p1, (char **)NULL, 10);
264 		*imonth = d;
265 		strcpy(month, getmonthname(d));
266 
267 		strcpy(dayofweek, getdayofweekname(offset));
268 		if (strlen(p2) == len)
269 			goto allfine;
270 		strcpy(modifierindex, p2 + len);
271 		*flags |= F_MODIFIERINDEX;
272 		goto allfine;
273 	}
274 
275 	/* If both the month and date are specified as numbers */
276 	if (isonlydigits(p1, 1) && isonlydigits(p2, 0)) {
277 		/* Now who wants to be this ambiguous? :-( */
278 		int m, d;
279 
280 		if (strchr(p2, '*') != NULL)
281 			*flags |= F_VARIABLE;
282 
283 		m = (int)strtol(p1, (char **)NULL, 10);
284 		d = (int)strtol(p2, (char **)NULL, 10);
285 
286 		*flags |= F_MONTH;
287 		*flags |= F_DAYOFMONTH;
288 
289 		if (m > 12) {
290 			*imonth = d;
291 			*idayofmonth = m;
292 			strcpy(month, getmonthname(d));
293 			sprintf(dayofmonth, "%d", m);
294 		} else {
295 			*imonth = m;
296 			*idayofmonth = d;
297 			strcpy(month, getmonthname(m));
298 			sprintf(dayofmonth, "%d", d);
299 		}
300 		goto allfine;
301 	}
302 
303 	/* FALLTHROUGH */
304 fail:
305 	*p = pold;
306 	return (0);
307 allfine:
308 	*p = pold;
309 	return (1);
310 
311 }
312 
313 void
314 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
315     int dd, char *extra);
316 void
317 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
318     int dd, char *extra)
319 {
320 	static int warned = 0;
321 
322 	if (*rememberindex >= MAXCOUNT - 1) {
323 		if (warned == 0)
324 			warnx("Index > %d, ignored", MAXCOUNT);
325 		warned++;
326 		return;
327 	}
328 	y[*rememberindex] = yy;
329 	m[*rememberindex] = mm;
330 	d[*rememberindex] = dd;
331 	if (extra != NULL)
332 		strcpy(ed[*rememberindex], extra);
333 	else
334 		ed[*rememberindex][0] = '\0';
335 	*rememberindex += 1;
336 }
337 
338 static void
339 debug_determinestyle(int dateonly, char *date, int flags, char *month,
340     int imonth, char *dayofmonth, int idayofmonth, char *dayofweek,
341     int idayofweek, char *modifieroffset, char *modifierindex, char *specialday,
342     char *year, int iyear)
343 {
344 
345 	if (dateonly != 0) {
346 		printf("-------\ndate: |%s|\n", date);
347 		if (dateonly == 1)
348 			return;
349 	}
350 	printf("flags: %x - %s\n", flags, showflags(flags));
351 	if (modifieroffset[0] != '\0')
352 		printf("modifieroffset: |%s|\n", modifieroffset);
353 	if (modifierindex[0] != '\0')
354 		printf("modifierindex: |%s|\n", modifierindex);
355 	if (year[0] != '\0')
356 		printf("year: |%s| (%d)\n", year, iyear);
357 	if (month[0] != '\0')
358 		printf("month: |%s| (%d)\n", month, imonth);
359 	if (dayofmonth[0] != '\0')
360 		printf("dayofmonth: |%s| (%d)\n", dayofmonth, idayofmonth);
361 	if (dayofweek[0] != '\0')
362 		printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
363 	if (specialday[0] != '\0')
364 		printf("specialday: |%s|\n", specialday);
365 }
366 
367 static struct yearinfo {
368 	int year;
369 	int ieaster, ipaskha, firstcnyday;
370 	double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
371 	double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
372 	int ichinesemonths[MAXMOONS];
373 	double equinoxdays[2], solsticedays[2];
374 	int *monthdays;
375 	struct yearinfo *next;
376 } *years, *yearinfo;
377 
378 /*
379  * Calculate dates with offset from weekdays, like Thurs-3, Wed+2, etc.
380  * day is the day of the week,
381  * offset the ordinal number of the weekday in the month.
382  */
383 static int
384 wdayom (int day, int offset, int month, int year)
385 {
386 /* Weekday of first day in month */
387 	int wday1;                                /* first day of month */
388 /* Weekday of last day in month */
389 	int wdayn;
390 	int d;
391 
392 	wday1 = first_dayofweek_of_month(year, month);
393 	if (wday1 < 0)                          /* not set */
394 		return (wday1);
395 	/*
396 	 * Date of zeroth or first of our weekday in month, depending on the
397 	 * relationship with the first of the month.  The range is -6:6.
398 	 */
399 	d = (day - wday1 + 1) % 7;
400 	/*
401 	 * Which way are we counting?  Offset 0 is invalid, abs (offset) > 5 is
402 	 * meaningless, but that's OK.  Offset 5 may or may not be meaningless,
403 	 * so there's no point in complaining for complaining's sake.
404 	 */
405 	if (offset < 0) {			/* back from end of month */
406 						/* FIXME */
407 		wdayn = d;
408 		while (wdayn <= yearinfo->monthdays[month])
409 			wdayn += 7;
410 		d = offset * 7 + wdayn;
411 	} else if (offset > 0){
412 		if (d > 0)
413 			d += offset * 7 - 7;
414 		else
415 			d += offset * 7;
416 	} else
417 		warnx ("Invalid offset 0");
418 	return (d);
419 }
420 
421 /*
422  * Possible date formats include any combination of:
423  *	3-charmonth			(January, Jan, Jan)
424  *	3-charweekday			(Friday, Monday, mon.)
425  *	numeric month or day		(1, 2, 04)
426  *
427  * Any character may separate them, or they may not be separated.  Any line,
428  * following a line that is matched, that starts with "whitespace", is shown
429  * along with the matched line.
430  */
431 int
432 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
433     char **edp)
434 {
435 	char month[100], dayofmonth[100], dayofweek[100], modifieroffset[100];
436 	char syear[100];
437 	char modifierindex[100], specialday[100];
438 	int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
439 	int year, remindex;
440 	int d, m, dow, rm, rd, offset;
441 	char *ed;
442 	int retvalsign = 1;
443 
444 	/*
445 	 * CONVENTION
446 	 *
447 	 * Month:     1-12
448 	 * Monthname: Jan .. Dec
449 	 * Day:	      1-31
450 	 * Weekday:   Mon .. Sun
451 	 *
452 	 */
453 
454 	*flags = 0;
455 
456 	if (debug)
457 		debug_determinestyle(1, date, *flags, month, imonth,
458 		    dayofmonth, idayofmonth, dayofweek, idayofweek,
459 		    modifieroffset, modifierindex, specialday, syear, iyear);
460 	if (determinestyle(date, flags, month, &imonth, dayofmonth,
461 		&idayofmonth, dayofweek, &idayofweek, modifieroffset,
462 		modifierindex, specialday, syear, &iyear) == 0) {
463 		if (debug)
464 			printf("Failed!\n");
465 		return (0);
466 	}
467 
468 	if (debug)
469 		debug_determinestyle(0, date, *flags, month, imonth,
470 		    dayofmonth, idayofmonth, dayofweek, idayofweek,
471 		    modifieroffset, modifierindex, specialday, syear, iyear);
472 
473 	remindex = 0;
474 	for (year = year1; year <= year2; year++) {
475 
476 		int lflags = *flags;
477 		/* If the year is specified, only do it if it is this year! */
478 		if ((lflags & F_YEAR) != 0)
479 			if (iyear != year)
480 				continue;
481 		lflags &= ~F_YEAR;
482 
483 		/* Get important dates for this year */
484 		yearinfo = years;
485 		while (yearinfo != NULL) {
486 			if (yearinfo->year == year)
487 				break;
488 			yearinfo = yearinfo -> next;
489 		}
490 		if (yearinfo == NULL) {
491 			yearinfo = (struct yearinfo *)calloc(1,
492 			    sizeof(struct yearinfo));
493 			if (yearinfo == NULL)
494 				errx(1, "Unable to allocate more years");
495 			yearinfo->year = year;
496 			yearinfo->next = years;
497 			years = yearinfo;
498 
499 			yearinfo->monthdays = monthdaytab[isleap(year)];
500 			yearinfo->ieaster = easter(year);
501 			yearinfo->ipaskha = paskha(year);
502 			fpom(year, UTCOffset, yearinfo->ffullmoon,
503 			    yearinfo->fnewmoon);
504 			fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
505 			    yearinfo->fnewmooncny);
506 			fequinoxsolstice(year, UTCOffset,
507 			    yearinfo->equinoxdays, yearinfo->solsticedays);
508 
509 			/*
510 			 * CNY: Match day with sun longitude at 330` with new
511 			 * moon
512 			 */
513 			yearinfo->firstcnyday = calculatesunlongitude30(year,
514 			    UTCOFFSET_CNY, yearinfo->ichinesemonths);
515 			for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) {
516 				if (yearinfo->fnewmooncny[m] >
517 				    yearinfo->firstcnyday) {
518 					yearinfo->firstcnyday =
519 					    floor(yearinfo->fnewmooncny[m - 1]);
520 					break;
521 				}
522 			}
523 		}
524 
525 		/* Same day every year */
526 		if (lflags == (F_MONTH | F_DAYOFMONTH)) {
527 			if (!remember_ymd(year, imonth, idayofmonth))
528 				continue;
529 			remember(&remindex, yearp, monthp, dayp, edp,
530 			    year, imonth, idayofmonth, NULL);
531 			continue;
532 		}
533 
534 		/* XXX Same day every year, but variable */
535 		if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
536 			if (!remember_ymd(year, imonth, idayofmonth))
537 				continue;
538 			remember(&remindex, yearp, monthp, dayp, edp,
539 			    year, imonth, idayofmonth, NULL);
540 			continue;
541 		}
542 
543 		/* Same day every month */
544 		if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) {
545 			for (m = 1; m <= 12; m++) {
546 				if (!remember_ymd(year, m, idayofmonth))
547 					continue;
548 				remember(&remindex, yearp, monthp, dayp, edp,
549 				    year, m, idayofmonth, NULL);
550 			}
551 			continue;
552 		}
553 
554 		/* Every day of a month */
555 		if (lflags == (F_ALLDAY | F_MONTH)) {
556 			for (d = 1; d <= yearinfo->monthdays[imonth]; d++) {
557 				if (!remember_ymd(year, imonth, d))
558 					continue;
559 				remember(&remindex, yearp, monthp, dayp, edp,
560 				    year, imonth, d, NULL);
561 			}
562 			continue;
563 		}
564 
565 		/* One day of every month */
566 		if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) {
567 			for (m = 1; m <= 12; m++) {
568 				if (!remember_ymd(year, m, idayofmonth))
569 					continue;
570 				remember(&remindex, yearp, monthp, dayp, edp,
571 				    year, m, idayofmonth, NULL);
572 			}
573 			continue;
574 		}
575 
576 		/* Every dayofweek of the year */
577 		if (lflags == (F_DAYOFWEEK | F_VARIABLE)) {
578 			dow = first_dayofweek_of_year(year);
579 			d = (idayofweek - dow + 8) % 7;
580 			while (d <= 366) {
581 				if (remember_yd(year, d, &rm, &rd))
582 					remember(&remindex,
583 					    yearp, monthp, dayp, edp,
584 					    year, rm, rd, NULL);
585 				d += 7;
586 			}
587 			continue;
588 		}
589 
590 		/*
591 	         * Every so-manied dayofweek of every month of the year:
592 	         * Thu-3
593 	         */
594 		if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
595 			offset = indextooffset(modifierindex);
596 
597 			for (m = 0; m <= 12; m++) {
598 	                        d = wdayom (idayofweek, offset, m, year);
599 				if (remember_ymd(year, m, d)) {
600 					remember(&remindex,
601 					    yearp, monthp, dayp, edp,
602 					    year, m, d, NULL);
603 					continue;
604 				}
605 			}
606 			continue;
607 		}
608 
609 		/*
610 	         * A certain dayofweek of a month
611 	         * Jan/Thu-3
612 	         */
613 		if (lflags ==
614 		    (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
615 			offset = indextooffset(modifierindex);
616 			dow = first_dayofweek_of_month(year, imonth);
617 			d = (idayofweek - dow + 8) % 7;
618 
619 			if (offset > 0) {
620 				while (d <= yearinfo->monthdays[imonth]) {
621 					if (--offset == 0
622 					    && remember_ymd(year, imonth, d)) {
623 						remember(&remindex,
624 						    yearp, monthp, dayp, edp,
625 						    year, imonth, d, NULL);
626 						continue;
627 					}
628 					d += 7;
629 				}
630 				continue;
631 			}
632 			if (offset < 0) {
633 				while (d <= yearinfo->monthdays[imonth])
634 					d += 7;
635 				while (offset != 0) {
636 					offset++;
637 					d -= 7;
638 				}
639 				if (remember_ymd(year, imonth, d))
640 					remember(&remindex,
641 					    yearp, monthp, dayp, edp,
642 					    year, imonth, d, NULL);
643 				continue;
644 			}
645 			continue;
646 		}
647 
648 		/* Every dayofweek of the month */
649 		if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
650 			dow = first_dayofweek_of_month(year, imonth);
651 			d = (idayofweek - dow + 8) % 7;
652 			while (d <= yearinfo->monthdays[imonth]) {
653 				if (remember_ymd(year, imonth, d))
654 					remember(&remindex,
655 					    yearp, monthp, dayp, edp,
656 					    year, imonth, d, NULL);
657 				d += 7;
658 			}
659 			continue;
660 		}
661 
662 		/* Easter */
663 		if ((lflags & ~F_MODIFIEROFFSET) ==
664 		    (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
665 			offset = 0;
666 			if ((lflags & F_MODIFIEROFFSET) != 0)
667 				offset = parseoffset(modifieroffset);
668 			if (remember_yd(year, yearinfo->ieaster + offset,
669 	                        &rm, &rd))
670 				remember(&remindex, yearp, monthp, dayp, edp,
671 				    year, rm, rd, NULL);
672 			continue;
673 		}
674 
675 		/* Paskha */
676 		if ((lflags & ~F_MODIFIEROFFSET) ==
677 		    (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
678 			offset = 0;
679 			if ((lflags & F_MODIFIEROFFSET) != 0)
680 				offset = parseoffset(modifieroffset);
681 			if (remember_yd(year, yearinfo->ipaskha + offset,
682 	                        &rm, &rd))
683 				remember(&remindex, yearp, monthp, dayp, edp,
684 				    year, rm, rd, NULL);
685 			continue;
686 		}
687 
688 		/* Chinese New Year */
689 		if ((lflags & ~F_MODIFIEROFFSET) ==
690 		    (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
691 			offset = 0;
692 			if ((lflags & F_MODIFIEROFFSET) != 0)
693 				offset = parseoffset(modifieroffset);
694 			if (remember_yd(year, yearinfo->firstcnyday + offset,
695 	                        &rm, &rd))
696 				remember(&remindex, yearp, monthp, dayp, edp,
697 				    year, rm, rd, NULL);
698 			continue;
699 		}
700 
701 		/* FullMoon */
702 		if ((lflags & ~F_MODIFIEROFFSET) ==
703 		    (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
704 			int i;
705 
706 			offset = 0;
707 			if ((lflags & F_MODIFIEROFFSET) != 0)
708 				offset = parseoffset(modifieroffset);
709 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
710 				if (remember_yd(year,
711 	                                floor(yearinfo->ffullmoon[i]) + offset,
712 					&rm, &rd)) {
713 					ed = floattotime(
714 					    yearinfo->ffullmoon[i]);
715 					remember(&remindex,
716 					    yearp, monthp, dayp, edp,
717 					    year, rm, rd, ed);
718 				}
719 			}
720 			continue;
721 		}
722 
723 		/* NewMoon */
724 		if ((lflags & ~F_MODIFIEROFFSET) ==
725 		    (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
726 			int i;
727 
728 			offset = 0;
729 			if ((lflags & F_MODIFIEROFFSET) != 0)
730 				offset = parseoffset(modifieroffset);
731 			for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
732 				if (remember_yd(year,
733 					floor(yearinfo->fnewmoon[i]) + offset,
734 					&rm, &rd)) {
735 					ed = floattotime(yearinfo->fnewmoon[i]);
736 					remember(&remindex,
737 					    yearp, monthp, dayp, edp,
738 					    year, rm, rd, ed);
739 				}
740 			}
741 			continue;
742 		}
743 
744 		/* (Mar|Sep)Equinox */
745 		if ((lflags & ~F_MODIFIEROFFSET) ==
746 		    (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
747 			offset = 0;
748 			if ((lflags & F_MODIFIEROFFSET) != 0)
749 				offset = parseoffset(modifieroffset);
750 			if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
751 				&rm, &rd)) {
752 				ed = floattotime(yearinfo->equinoxdays[0]);
753 				remember(&remindex, yearp, monthp, dayp, edp,
754 				    year, rm, rd, ed);
755 			}
756 			continue;
757 		}
758 		if ((lflags & ~F_MODIFIEROFFSET) ==
759 		    (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
760 			offset = 0;
761 			if ((lflags & F_MODIFIEROFFSET) != 0)
762 				offset = parseoffset(modifieroffset);
763 			if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
764 			    &rm, &rd)) {
765 				ed = floattotime(yearinfo->equinoxdays[1]);
766 				remember(&remindex, yearp, monthp, dayp, edp,
767 				    year, rm, rd, ed);
768 			}
769 			continue;
770 		}
771 
772 		/* (Jun|Dec)Solstice */
773 		if ((lflags & ~F_MODIFIEROFFSET) ==
774 		    (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
775 			offset = 0;
776 			if ((lflags & F_MODIFIEROFFSET) != 0)
777 				offset = parseoffset(modifieroffset);
778 			if (remember_yd(year,
779 				yearinfo->solsticedays[0] + offset, &rm, &rd)) {
780 				ed = floattotime(yearinfo->solsticedays[0]);
781 				remember(&remindex, yearp, monthp, dayp, edp,
782 				    year, rm, rd, ed);
783 			}
784 			continue;
785 		}
786 		if ((lflags & ~F_MODIFIEROFFSET) ==
787 		    (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
788 			offset = 0;
789 			if ((lflags & F_MODIFIEROFFSET) != 0)
790 				offset = parseoffset(modifieroffset);
791 			if (remember_yd(year,
792 				yearinfo->solsticedays[1] + offset, &rm, &rd)) {
793 				ed = floattotime(yearinfo->solsticedays[1]);
794 				remember(&remindex, yearp, monthp, dayp, edp,
795 				    year, rm, rd, ed);
796 			}
797 			continue;
798 		}
799 
800 		if (debug) {
801 			printf("Unprocessed:\n");
802 			debug_determinestyle(2, date, lflags, month, imonth,
803 			    dayofmonth, idayofmonth, dayofweek, idayofweek,
804 			    modifieroffset, modifierindex, specialday, syear,
805 			    iyear);
806 		}
807 		retvalsign = -1;
808 	}
809 
810 	if (retvalsign == -1)
811 		return (-remindex - 1);
812 	else
813 		return (remindex);
814 }
815 
816 static char *
817 showflags(int flags)
818 {
819 	static char s[1000];
820 	s[0] = '\0';
821 
822 	if ((flags & F_YEAR) != 0)
823 		strcat(s, "year ");
824 	if ((flags & F_MONTH) != 0)
825 		strcat(s, "month ");
826 	if ((flags & F_DAYOFWEEK) != 0)
827 		strcat(s, "dayofweek ");
828 	if ((flags & F_DAYOFMONTH) != 0)
829 		strcat(s, "dayofmonth ");
830 	if ((flags & F_MODIFIERINDEX) != 0)
831 		strcat(s, "modifierindex ");
832 	if ((flags & F_MODIFIEROFFSET) != 0)
833 		strcat(s, "modifieroffset ");
834 	if ((flags & F_SPECIALDAY) != 0)
835 		strcat(s, "specialday ");
836 	if ((flags & F_ALLMONTH) != 0)
837 		strcat(s, "allmonth ");
838 	if ((flags & F_ALLDAY) != 0)
839 		strcat(s, "allday ");
840 	if ((flags & F_VARIABLE) != 0)
841 		strcat(s, "variable ");
842 	if ((flags & F_CNY) != 0)
843 		strcat(s, "chinesenewyear ");
844 	if ((flags & F_PASKHA) != 0)
845 		strcat(s, "paskha ");
846 	if ((flags & F_EASTER) != 0)
847 		strcat(s, "easter ");
848 	if ((flags & F_FULLMOON) != 0)
849 		strcat(s, "fullmoon ");
850 	if ((flags & F_NEWMOON) != 0)
851 		strcat(s, "newmoon ");
852 	if ((flags & F_MAREQUINOX) != 0)
853 		strcat(s, "marequinox ");
854 	if ((flags & F_SEPEQUINOX) != 0)
855 		strcat(s, "sepequinox ");
856 	if ((flags & F_JUNSOLSTICE) != 0)
857 		strcat(s, "junsolstice ");
858 	if ((flags & F_DECSOLSTICE) != 0)
859 		strcat(s, "decsolstice ");
860 
861 	return s;
862 }
863 
864 static const char *
865 getmonthname(int i)
866 {
867 	if (i <= 0 || i > 12)
868 		return ("");
869 	if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
870 		return (nmonths[i - 1].name);
871 	return (months[i - 1]);
872 }
873 
874 static int
875 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
876 {
877 	struct fixs *n;
878 	int i;
879 
880 	for (i = 0; fnmonths[i].name != NULL; i++) {
881 		n = fnmonths + i;
882 		if (strncasecmp(s, n->name, n->len) == 0) {
883 			*len = n->len;
884 			*month = n->name;
885 			*offset = i + 1;
886 			return (1);
887 		}
888 	}
889 	for (i = 0; nmonths[i].name != NULL; i++) {
890 		n = nmonths + i;
891 		if (strncasecmp(s, n->name, n->len) == 0) {
892 			*len = n->len;
893 			*month = n->name;
894 			*offset = i + 1;
895 			return (1);
896 		}
897 	}
898 	for (i = 0; fmonths[i] != NULL; i++) {
899 		*len = strlen(fmonths[i]);
900 		if (strncasecmp(s, fmonths[i], *len) == 0) {
901 			*month = fmonths[i];
902 			*offset = i + 1;
903 			return (1);
904 		}
905 	}
906 	for (i = 0; months[i] != NULL; i++) {
907 		if (strncasecmp(s, months[i], 3) == 0) {
908 			*len = 3;
909 			*month = months[i];
910 			*offset = i + 1;
911 			return (1);
912 		}
913 	}
914 	return (0);
915 }
916 
917 static const char *
918 getdayofweekname(int i)
919 {
920 	if (ndays[i].len != 0 && ndays[i].name != NULL)
921 		return (ndays[i].name);
922 	return (days[i]);
923 }
924 
925 static int
926 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
927 {
928 	struct fixs *n;
929 	int i;
930 
931 	for (i = 0; fndays[i].name != NULL; i++) {
932 		n = fndays + i;
933 		if (strncasecmp(s, n->name, n->len) == 0) {
934 			*len = n->len;
935 			*dow = n->name;
936 			*offset = i;
937 			return (1);
938 		}
939 	}
940 	for (i = 0; ndays[i].name != NULL; i++) {
941 		n = ndays + i;
942 		if (strncasecmp(s, n->name, n->len) == 0) {
943 			*len = n->len;
944 			*dow = n->name;
945 			*offset = i;
946 			return (1);
947 		}
948 	}
949 	for (i = 0; fdays[i] != NULL; i++) {
950 		*len = strlen(fdays[i]);
951 		if (strncasecmp(s, fdays[i], *len) == 0) {
952 			*dow = fdays[i];
953 			*offset = i;
954 			return (1);
955 		}
956 	}
957 	for (i = 0; days[i] != NULL; i++) {
958 		if (strncasecmp(s, days[i], 3) == 0) {
959 			*len = 3;
960 			*dow = days[i];
961 			*offset = i;
962 			return (1);
963 		}
964 	}
965 	return (0);
966 }
967 
968 static int
969 isonlydigits(char *s, int nostar)
970 {
971 	int i;
972 	for (i = 0; s[i] != '\0'; i++) {
973 		if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
974 			return 1;
975 		if (!isdigit((unsigned char)s[i]))
976 			return (0);
977 	}
978 	return (1);
979 }
980 
981 static int
982 indextooffset(char *s)
983 {
984 	int i;
985 	struct fixs *n;
986 	char *es;
987 
988 	if (s[0] == '+' || s[0] == '-') {
989 		i = strtol (s, &es, 10);
990 		if (*es != '\0')                      /* trailing junk */
991 			errx (1, "Invalid specifier format: %s\n", s);
992 		return (i);
993 	}
994 
995 	for (i = 0; i < 6; i++) {
996 		if (strcasecmp(s, sequences[i]) == 0) {
997 			if (i == 5)
998 				return (-1);
999 			return (i + 1);
1000 		}
1001 	}
1002 	for (i = 0; i < 6; i++) {
1003 		n = nsequences + i;
1004 		if (n->len == 0)
1005 			continue;
1006 		if (strncasecmp(s, n->name, n->len) == 0) {
1007 			if (i == 5)
1008 				return (-1);
1009 			return (i + 1);
1010 		}
1011 	}
1012 	return (0);
1013 }
1014 
1015 static int
1016 parseoffset(char *s)
1017 {
1018 	return strtol(s, NULL, 10);
1019 }
1020 
1021 static char *
1022 floattotime(double f)
1023 {
1024 	static char buf[100];
1025 	int hh, mm, ss, i;
1026 
1027 	f -= floor(f);
1028 	i = f * SECSPERDAY;
1029 
1030 	hh = i / SECSPERHOUR;
1031 	i %= SECSPERHOUR;
1032 	mm = i / SECSPERMINUTE;
1033 	i %= SECSPERMINUTE;
1034 	ss = i;
1035 
1036 	sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
1037 	return (buf);
1038 }
1039 
1040 static char *
1041 floattoday(int year, double f)
1042 {
1043 	static char buf[100];
1044 	int i, m, d, hh, mm, ss;
1045 	int *cumdays = cumdaytab[isleap(year)];
1046 
1047 	for (i = 0; 1 + cumdays[i] < f; i++)
1048 		;
1049 	m = --i;
1050 	d = floor(f - 1 - cumdays[i]);
1051 	f -= floor(f);
1052 	i = f * SECSPERDAY;
1053 
1054 	hh = i / SECSPERHOUR;
1055 	i %= SECSPERHOUR;
1056 	mm = i / SECSPERMINUTE;
1057 	i %= SECSPERMINUTE;
1058 	ss = i;
1059 
1060 	sprintf(buf, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
1061 	return (buf);
1062 }
1063 
1064 void
1065 dodebug(char *what)
1066 {
1067 	int year;
1068 
1069 	printf("UTCOffset: %g\n", UTCOffset);
1070 	printf("eastlongitude: %d\n", EastLongitude);
1071 
1072 	if (strcmp(what, "moon") == 0) {
1073 		double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
1074 		int i;
1075 
1076 		for (year = year1; year <= year2; year++) {
1077 			fpom(year, UTCOffset, ffullmoon, fnewmoon);
1078 			printf("Full moon %d:\t", year);
1079 			for (i = 0; ffullmoon[i] >= 0; i++) {
1080 				printf("%g (%s) ", ffullmoon[i],
1081 				    floattoday(year, ffullmoon[i]));
1082 			}
1083 			printf("\nNew moon %d:\t", year);
1084 			for (i = 0; fnewmoon[i] >= 0; i++) {
1085 				printf("%g (%s) ", fnewmoon[i],
1086 				    floattoday(year, fnewmoon[i]));
1087 			}
1088 			printf("\n");
1089 
1090 		}
1091 
1092 		return;
1093 	}
1094 
1095 	if (strcmp(what, "sun") == 0) {
1096 		double equinoxdays[2], solsticedays[2];
1097 		for (year = year1; year <= year2; year++) {
1098 			printf("Sun in %d:\n", year);
1099 			fequinoxsolstice(year, UTCOffset, equinoxdays,
1100 			    solsticedays);
1101 			printf("e[0] - %g (%s)\n",
1102 			    equinoxdays[0],
1103 			    floattoday(year, equinoxdays[0]));
1104 			printf("e[1] - %g (%s)\n",
1105 			    equinoxdays[1],
1106 			    floattoday(year, equinoxdays[1]));
1107 			printf("s[0] - %g (%s)\n",
1108 			    solsticedays[0],
1109 			    floattoday(year, solsticedays[0]));
1110 			printf("s[1] - %g (%s)\n",
1111 			    solsticedays[1],
1112 			    floattoday(year, solsticedays[1]));
1113 		}
1114 		return;
1115 	}
1116 }
1117