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