2 * SPDX-License-Identifier: BSD-2-Clause
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>
40 #define SLEN 100 /* maximum length of date spec. part strings */
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);
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
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.'
74 * DayOfWeek ::= DayOfWeekShort | DayOfWeekLong
75 * DayOfWeekShort ::= 'Mon' .. 'Sun'
76 * DayOfWeekLong ::= 'Monday' .. 'Sunday'
77 * DayOfMonth ::= '0' ... '9' | '00' ... '09' | '10' ... '29' |
80 * ModifierOffset ::= '' | '+' . ModifierNumber | '-' . ModifierNumber
81 * ModifierNumber ::= '0' ... '9' | '00' ... '99' | '000' ... '299' |
82 * '300' ... '359' | '360' ... '365'
83 * ModifierIndex ::= 'Second' | 'Third' | 'Fourth' | 'Fifth' |
86 * SpecialDay ::= 'Easter' | 'Paskha' | 'ChineseNewYear'
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)
95 char *p, *p1, *p2, *py;
96 const char *dow, *pmonth;
109 *modifieroffset = '\0';
110 *modifierindex = '\0';
113 #define CHECKSPECIAL(s1, s2, lens2, type) \
114 if (s2 != NULL && strncmp(s1, s2, lens2) == 0) { \
115 *flags |= F_SPECIALDAY; \
117 *flags |= F_VARIABLE; \
118 if (strlen(s1) == lens2) { \
119 strlcpy(specialday, s1, SLEN); \
122 strncpy(specialday, s1, lens2); \
123 specialday[lens2] = '\0'; \
124 strlcpy(modifieroffset, s1 + lens2, SLEN); \
125 *flags |= F_MODIFIEROFFSET; \
129 if ((p = strchr(date, ' ')) == NULL) {
130 if ((p = strchr(date, '/')) == NULL) {
131 CHECKSPECIAL(date, STRING_CNY, strlen(STRING_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,
138 CHECKSPECIAL(date, STRING_FULLMOON,
139 strlen(STRING_FULLMOON), F_FULLMOON);
140 CHECKSPECIAL(date, nfullmoon.name, nfullmoon.len,
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,
152 CHECKSPECIAL(date, STRING_SEPEQUINOX,
153 strlen(STRING_SEPEQUINOX), F_SEPEQUINOX);
154 CHECKSPECIAL(date, nsepequinox.name, nsepequinox.len,
156 CHECKSPECIAL(date, STRING_JUNSOLSTICE,
157 strlen(STRING_JUNSOLSTICE), F_JUNSOLSTICE);
158 CHECKSPECIAL(date, njunsolstice.name, njunsolstice.len,
160 CHECKSPECIAL(date, STRING_DECSOLSTICE,
161 strlen(STRING_DECSOLSTICE), F_DECSOLSTICE);
162 CHECKSPECIAL(date, ndecsolstice.name, ndecsolstice.len,
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 strlcpy(dayofweek, date, SLEN);
172 strncpy(dayofweek, date, len);
173 dayofweek[len] = '\0';
174 strlcpy(modifierindex, date + len, SLEN);
175 *flags |= F_MODIFIERINDEX;
178 if (isonlydigits(date, 1)) {
179 /* Assume month number only */
181 *imonth = (int)strtol(date, (char **)NULL, 10);
182 strlcpy(month, getmonthname(*imonth), SLEN);
190 * After this, leave by goto-ing to "allfine" or "fail" to restore the
191 * original data in `date'.
197 /* Now p2 points to the next field and p1 to the first field */
199 if ((py = strchr(p2, '/')) != NULL) {
200 /* We have a year in the string. Now this is getting tricky */
201 strlcpy(year, p1, SLEN);
202 *iyear = (int)strtol(year, NULL, 10);
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 */
216 strlcpy(month, getmonthname(offset), SLEN);
217 if (isonlydigits(p2, 1)) {
218 strlcpy(dayofmonth, p2, SLEN);
219 *idayofmonth = (int)strtol(p2, (char **)NULL, 10);
220 *flags |= F_DAYOFMONTH;
223 if (strcmp(p2, "*") == 0) {
228 if (checkdayofweek(p2, &len, &offset, &dow) != 0) {
229 *flags |= F_DAYOFWEEK;
230 *flags |= F_VARIABLE;
231 *idayofweek = offset;
232 strlcpy(dayofweek, getdayofweekname(offset), SLEN);
233 if (strlen(p2) == len)
235 strlcpy(modifierindex, p2 + len, SLEN);
236 *flags |= F_MODIFIERINDEX;
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))) {
247 *flags |= F_ALLMONTH;
248 *flags |= F_DAYOFMONTH;
249 d = (int)strtol(p2, (char **)NULL, 10);
251 snprintf(dayofmonth, SLEN, "%d", d);
255 /* Month as a number, then a weekday */
256 if (isonlydigits(p1, 1)
257 && checkdayofweek(p2, &len, &offset, &dow) != 0) {
261 *flags |= F_DAYOFWEEK;
262 *flags |= F_VARIABLE;
264 *idayofweek = offset;
265 d = (int)strtol(p1, (char **)NULL, 10);
267 strlcpy(month, getmonthname(d), SLEN);
269 strlcpy(dayofweek, getdayofweekname(offset), SLEN);
270 if (strlen(p2) == len)
272 strlcpy(modifierindex, p2 + len, SLEN);
273 *flags |= F_MODIFIERINDEX;
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? :-( */
282 if (strchr(p2, '*') != NULL)
283 *flags |= F_VARIABLE;
285 m = (int)strtol(p1, (char **)NULL, 10);
286 d = (int)strtol(p2, (char **)NULL, 10);
289 *flags |= F_DAYOFMONTH;
294 strlcpy(month, getmonthname(d), SLEN);
295 snprintf(dayofmonth, SLEN, "%d", m);
299 strlcpy(month, getmonthname(m), SLEN);
300 snprintf(dayofmonth, SLEN, "%d", d);
316 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
319 static int warned = 0;
321 if (*rememberindex >= MAXCOUNT - 1) {
323 warnx("Index > %d, ignored", MAXCOUNT);
327 y[*rememberindex] = yy;
328 m[*rememberindex] = mm;
329 d[*rememberindex] = dd;
331 strlcpy(ed[*rememberindex], extra, SLEN);
333 ed[*rememberindex][0] = '\0';
338 debug_determinestyle(int dateonly, char *date, int flags, char *month,
339 int imonth, char *dayofmonth, int idayofmonth, char *dayofweek,
340 int idayofweek, char *modifieroffset, char *modifierindex, char *specialday,
341 char *year, int iyear)
345 printf("-------\ndate: |%s|\n", date);
349 printf("flags: %x - %s\n", flags, showflags(flags));
350 if (modifieroffset[0] != '\0')
351 printf("modifieroffset: |%s|\n", modifieroffset);
352 if (modifierindex[0] != '\0')
353 printf("modifierindex: |%s|\n", modifierindex);
355 printf("year: |%s| (%d)\n", year, iyear);
356 if (month[0] != '\0')
357 printf("month: |%s| (%d)\n", month, imonth);
358 if (dayofmonth[0] != '\0')
359 printf("dayofmonth: |%s| (%d)\n", dayofmonth, idayofmonth);
360 if (dayofweek[0] != '\0')
361 printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
362 if (specialday[0] != '\0')
363 printf("specialday: |%s|\n", specialday);
366 static struct yearinfo {
368 int ieaster, ipaskha, firstcnyday;
369 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
370 double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
371 int ichinesemonths[MAXMOONS];
372 double equinoxdays[2], solsticedays[2];
374 struct yearinfo *next;
378 * Calculate dates with offset from weekdays, like Thurs-3, Wed+2, etc.
379 * day is the day of the week,
380 * offset the ordinal number of the weekday in the month.
383 wdayom (int day, int offset, int month, int year)
385 /* Weekday of first day in month */
386 int wday1; /* first day of month */
387 /* Weekday of last day in month */
391 wday1 = first_dayofweek_of_month(year, month);
392 if (wday1 < 0) /* not set */
395 * Date of zeroth or first of our weekday in month, depending on the
396 * relationship with the first of the month. The range is -6:6.
398 d = (day - wday1 + 1) % 7;
400 * Which way are we counting? Offset 0 is invalid, abs (offset) > 5 is
401 * meaningless, but that's OK. Offset 5 may or may not be meaningless,
402 * so there's no point in complaining for complaining's sake.
404 if (offset < 0) { /* back from end of month */
407 while (wdayn <= yearinfo->monthdays[month])
409 d = offset * 7 + wdayn;
410 } else if (offset > 0){
416 warnx ("Invalid offset 0");
421 * Possible date formats include any combination of:
422 * 3-charmonth (January, Jan, Jan)
423 * 3-charweekday (Friday, Monday, mon.)
424 * numeric month or day (1, 2, 04)
426 * Any character may separate them, or they may not be separated. Any line,
427 * following a line that is matched, that starts with "whitespace", is shown
428 * along with the matched line.
431 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
434 char month[SLEN], dayofmonth[SLEN], dayofweek[SLEN], modifieroffset[SLEN];
436 char modifierindex[SLEN], specialday[SLEN];
437 int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
439 int d, m, dow, rm, rd, offset;
447 * Monthname: Jan .. Dec
449 * Weekday: Mon .. Sun
456 debug_determinestyle(1, date, *flags, month, imonth,
457 dayofmonth, idayofmonth, dayofweek, idayofweek,
458 modifieroffset, modifierindex, specialday, syear, iyear);
459 if (determinestyle(date, flags, month, &imonth, dayofmonth,
460 &idayofmonth, dayofweek, &idayofweek, modifieroffset,
461 modifierindex, specialday, syear, &iyear) == 0) {
468 debug_determinestyle(0, date, *flags, month, imonth,
469 dayofmonth, idayofmonth, dayofweek, idayofweek,
470 modifieroffset, modifierindex, specialday, syear, iyear);
473 for (year = year1; year <= year2; year++) {
476 /* If the year is specified, only do it if it is this year! */
477 if ((lflags & F_YEAR) != 0)
482 /* Get important dates for this year */
484 while (yearinfo != NULL) {
485 if (yearinfo->year == year)
487 yearinfo = yearinfo -> next;
489 if (yearinfo == NULL) {
490 yearinfo = (struct yearinfo *)calloc(1,
491 sizeof(struct yearinfo));
492 if (yearinfo == NULL)
493 errx(1, "Unable to allocate more years");
494 yearinfo->year = year;
495 yearinfo->next = years;
498 yearinfo->monthdays = monthdaytab[isleap(year)];
499 yearinfo->ieaster = easter(year);
500 yearinfo->ipaskha = paskha(year);
501 fpom(year, UTCOffset, yearinfo->ffullmoon,
503 fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
504 yearinfo->fnewmooncny);
505 fequinoxsolstice(year, UTCOffset,
506 yearinfo->equinoxdays, yearinfo->solsticedays);
509 * CNY: Match day with sun longitude at 330` with new
512 yearinfo->firstcnyday = calculatesunlongitude30(year,
513 UTCOFFSET_CNY, yearinfo->ichinesemonths);
514 for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) {
515 if (yearinfo->fnewmooncny[m] >
516 yearinfo->firstcnyday) {
517 yearinfo->firstcnyday =
518 floor(yearinfo->fnewmooncny[m - 1]);
524 /* Same day every year */
525 if (lflags == (F_MONTH | F_DAYOFMONTH)) {
526 if (!remember_ymd(year, imonth, idayofmonth))
528 remember(&remindex, yearp, monthp, dayp, edp,
529 year, imonth, idayofmonth, NULL);
533 /* XXX Same day every year, but variable */
534 if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
535 if (!remember_ymd(year, imonth, idayofmonth))
537 remember(&remindex, yearp, monthp, dayp, edp,
538 year, imonth, idayofmonth, NULL);
542 /* Same day every month */
543 if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) {
544 for (m = 1; m <= 12; m++) {
545 if (!remember_ymd(year, m, idayofmonth))
547 remember(&remindex, yearp, monthp, dayp, edp,
548 year, m, idayofmonth, NULL);
553 /* Every day of a month */
554 if (lflags == (F_ALLDAY | F_MONTH)) {
555 for (d = 1; d <= yearinfo->monthdays[imonth]; d++) {
556 if (!remember_ymd(year, imonth, d))
558 remember(&remindex, yearp, monthp, dayp, edp,
559 year, imonth, d, NULL);
564 /* One day of every month */
565 if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) {
566 for (m = 1; m <= 12; m++) {
567 if (!remember_ymd(year, m, idayofmonth))
569 remember(&remindex, yearp, monthp, dayp, edp,
570 year, m, idayofmonth, NULL);
575 /* Every dayofweek of the year */
576 if (lflags == (F_DAYOFWEEK | F_VARIABLE)) {
577 dow = first_dayofweek_of_year(year);
580 d = (idayofweek - dow + 7) % 7 + 1;
582 if (remember_yd(year, d, &rm, &rd))
584 yearp, monthp, dayp, edp,
592 * Every so-manied dayofweek of every month of the year:
595 if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
596 offset = indextooffset(modifierindex);
598 for (m = 0; m <= 12; m++) {
599 d = wdayom (idayofweek, offset, m, year);
600 if (remember_ymd(year, m, d)) {
602 yearp, monthp, dayp, edp,
611 * A certain dayofweek of a month
615 (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
616 offset = indextooffset(modifierindex);
617 dow = first_dayofweek_of_month(year, imonth);
620 d = (idayofweek - dow + 7) % 7 + 1;
623 while (d <= yearinfo->monthdays[imonth]) {
625 && remember_ymd(year, imonth, d)) {
627 yearp, monthp, dayp, edp,
628 year, imonth, d, NULL);
636 while (d <= yearinfo->monthdays[imonth])
638 while (offset != 0) {
642 if (remember_ymd(year, imonth, d))
644 yearp, monthp, dayp, edp,
645 year, imonth, d, NULL);
651 /* Every dayofweek of the month */
652 if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
653 dow = first_dayofweek_of_month(year, imonth);
656 d = (idayofweek - dow + 7) % 7 + 1;
657 while (d <= yearinfo->monthdays[imonth]) {
658 if (remember_ymd(year, imonth, d))
660 yearp, monthp, dayp, edp,
661 year, imonth, d, NULL);
668 if ((lflags & ~F_MODIFIEROFFSET) ==
669 (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
671 if ((lflags & F_MODIFIEROFFSET) != 0)
672 offset = parseoffset(modifieroffset);
673 if (remember_yd(year, yearinfo->ieaster + offset,
675 remember(&remindex, yearp, monthp, dayp, edp,
681 if ((lflags & ~F_MODIFIEROFFSET) ==
682 (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
684 if ((lflags & F_MODIFIEROFFSET) != 0)
685 offset = parseoffset(modifieroffset);
686 if (remember_yd(year, yearinfo->ipaskha + offset,
688 remember(&remindex, yearp, monthp, dayp, edp,
693 /* Chinese New Year */
694 if ((lflags & ~F_MODIFIEROFFSET) ==
695 (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
697 if ((lflags & F_MODIFIEROFFSET) != 0)
698 offset = parseoffset(modifieroffset);
699 if (remember_yd(year, yearinfo->firstcnyday + offset,
701 remember(&remindex, yearp, monthp, dayp, edp,
707 if ((lflags & ~F_MODIFIEROFFSET) ==
708 (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
712 if ((lflags & F_MODIFIEROFFSET) != 0)
713 offset = parseoffset(modifieroffset);
714 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
715 if (remember_yd(year,
716 floor(yearinfo->ffullmoon[i]) + offset,
719 yearinfo->ffullmoon[i]);
721 yearp, monthp, dayp, edp,
729 if ((lflags & ~F_MODIFIEROFFSET) ==
730 (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
734 if ((lflags & F_MODIFIEROFFSET) != 0)
735 offset = parseoffset(modifieroffset);
736 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
737 if (remember_yd(year,
738 floor(yearinfo->fnewmoon[i]) + offset,
740 ed = floattotime(yearinfo->fnewmoon[i]);
742 yearp, monthp, dayp, edp,
749 /* (Mar|Sep)Equinox */
750 if ((lflags & ~F_MODIFIEROFFSET) ==
751 (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
753 if ((lflags & F_MODIFIEROFFSET) != 0)
754 offset = parseoffset(modifieroffset);
755 if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
757 ed = floattotime(yearinfo->equinoxdays[0]);
758 remember(&remindex, yearp, monthp, dayp, edp,
763 if ((lflags & ~F_MODIFIEROFFSET) ==
764 (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
766 if ((lflags & F_MODIFIEROFFSET) != 0)
767 offset = parseoffset(modifieroffset);
768 if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
770 ed = floattotime(yearinfo->equinoxdays[1]);
771 remember(&remindex, yearp, monthp, dayp, edp,
777 /* (Jun|Dec)Solstice */
778 if ((lflags & ~F_MODIFIEROFFSET) ==
779 (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
781 if ((lflags & F_MODIFIEROFFSET) != 0)
782 offset = parseoffset(modifieroffset);
783 if (remember_yd(year,
784 yearinfo->solsticedays[0] + offset, &rm, &rd)) {
785 ed = floattotime(yearinfo->solsticedays[0]);
786 remember(&remindex, yearp, monthp, dayp, edp,
791 if ((lflags & ~F_MODIFIEROFFSET) ==
792 (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
794 if ((lflags & F_MODIFIEROFFSET) != 0)
795 offset = parseoffset(modifieroffset);
796 if (remember_yd(year,
797 yearinfo->solsticedays[1] + offset, &rm, &rd)) {
798 ed = floattotime(yearinfo->solsticedays[1]);
799 remember(&remindex, yearp, monthp, dayp, edp,
806 printf("Unprocessed:\n");
807 debug_determinestyle(2, date, lflags, month, imonth,
808 dayofmonth, idayofmonth, dayofweek, idayofweek,
809 modifieroffset, modifierindex, specialday, syear,
815 if (retvalsign == -1)
816 return (-remindex - 1);
827 if ((flags & F_YEAR) != 0)
828 strlcat(s, "year ", SLEN);
829 if ((flags & F_MONTH) != 0)
830 strlcat(s, "month ", SLEN);
831 if ((flags & F_DAYOFWEEK) != 0)
832 strlcat(s, "dayofweek ", SLEN);
833 if ((flags & F_DAYOFMONTH) != 0)
834 strlcat(s, "dayofmonth ", SLEN);
835 if ((flags & F_MODIFIERINDEX) != 0)
836 strlcat(s, "modifierindex ", SLEN);
837 if ((flags & F_MODIFIEROFFSET) != 0)
838 strlcat(s, "modifieroffset ", SLEN);
839 if ((flags & F_SPECIALDAY) != 0)
840 strlcat(s, "specialday ", SLEN);
841 if ((flags & F_ALLMONTH) != 0)
842 strlcat(s, "allmonth ", SLEN);
843 if ((flags & F_ALLDAY) != 0)
844 strlcat(s, "allday ", SLEN);
845 if ((flags & F_VARIABLE) != 0)
846 strlcat(s, "variable ", SLEN);
847 if ((flags & F_CNY) != 0)
848 strlcat(s, "chinesenewyear ", SLEN);
849 if ((flags & F_PASKHA) != 0)
850 strlcat(s, "paskha ", SLEN);
851 if ((flags & F_EASTER) != 0)
852 strlcat(s, "easter ", SLEN);
853 if ((flags & F_FULLMOON) != 0)
854 strlcat(s, "fullmoon ", SLEN);
855 if ((flags & F_NEWMOON) != 0)
856 strlcat(s, "newmoon ", SLEN);
857 if ((flags & F_MAREQUINOX) != 0)
858 strlcat(s, "marequinox ", SLEN);
859 if ((flags & F_SEPEQUINOX) != 0)
860 strlcat(s, "sepequinox ", SLEN);
861 if ((flags & F_JUNSOLSTICE) != 0)
862 strlcat(s, "junsolstice ", SLEN);
863 if ((flags & F_DECSOLSTICE) != 0)
864 strlcat(s, "decsolstice ", SLEN);
872 if (i <= 0 || i > 12)
874 if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
875 return (nmonths[i - 1].name);
876 return (months[i - 1]);
880 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
885 for (i = 0; fnmonths[i].name != NULL; i++) {
887 if (strncasecmp(s, n->name, n->len) == 0) {
894 for (i = 0; nmonths[i].name != NULL; i++) {
896 if (strncasecmp(s, n->name, n->len) == 0) {
903 for (i = 0; fmonths[i] != NULL; i++) {
904 *len = strlen(fmonths[i]);
905 if (strncasecmp(s, fmonths[i], *len) == 0) {
911 for (i = 0; months[i] != NULL; i++) {
912 if (strncasecmp(s, months[i], 3) == 0) {
923 getdayofweekname(int i)
925 if (ndays[i].len != 0 && ndays[i].name != NULL)
926 return (ndays[i].name);
931 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
936 for (i = 0; fndays[i].name != NULL; i++) {
938 if (strncasecmp(s, n->name, n->len) == 0) {
945 for (i = 0; ndays[i].name != NULL; i++) {
947 if (strncasecmp(s, n->name, n->len) == 0) {
954 for (i = 0; fdays[i] != NULL; i++) {
955 *len = strlen(fdays[i]);
956 if (strncasecmp(s, fdays[i], *len) == 0) {
962 for (i = 0; days[i] != NULL; i++) {
963 if (strncasecmp(s, days[i], 3) == 0) {
974 isonlydigits(char *s, int nostar)
977 for (i = 0; s[i] != '\0'; i++) {
978 if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
980 if (!isdigit((unsigned char)s[i]))
987 indextooffset(char *s)
993 if (s[0] == '+' || s[0] == '-') {
994 i = strtol (s, &es, 10);
995 if (*es != '\0') /* trailing junk */
996 errx (1, "Invalid specifier format: %s\n", s);
1000 for (i = 0; i < 6; i++) {
1001 if (strcasecmp(s, sequences[i]) == 0) {
1007 for (i = 0; i < 6; i++) {
1011 if (strncasecmp(s, n->name, n->len) == 0) {
1021 parseoffset(char *s)
1023 return strtol(s, NULL, 10);
1027 floattotime(double f)
1029 static char buf[SLEN];
1035 hh = i / SECSPERHOUR;
1037 mm = i / SECSPERMINUTE;
1041 snprintf(buf, SLEN, "%02d:%02d:%02d", hh, mm, ss);
1046 floattoday(int year, double f)
1048 static char buf[SLEN];
1049 int i, m, d, hh, mm, ss;
1050 int *cumdays = cumdaytab[isleap(year)];
1052 for (i = 0; 1 + cumdays[i] < f; i++)
1055 d = floor(f - 1 - cumdays[i]);
1059 hh = i / SECSPERHOUR;
1061 mm = i / SECSPERMINUTE;
1065 snprintf(buf, SLEN, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
1074 printf("UTCOffset: %g\n", UTCOffset);
1075 printf("eastlongitude: %d\n", EastLongitude);
1077 if (strcmp(what, "moon") == 0) {
1078 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
1081 for (year = year1; year <= year2; year++) {
1082 fpom(year, UTCOffset, ffullmoon, fnewmoon);
1083 printf("Full moon %d:\t", year);
1084 for (i = 0; ffullmoon[i] >= 0; i++) {
1085 printf("%g (%s) ", ffullmoon[i],
1086 floattoday(year, ffullmoon[i]));
1088 printf("\nNew moon %d:\t", year);
1089 for (i = 0; fnewmoon[i] >= 0; i++) {
1090 printf("%g (%s) ", fnewmoon[i],
1091 floattoday(year, fnewmoon[i]));
1100 if (strcmp(what, "sun") == 0) {
1101 double equinoxdays[2], solsticedays[2];
1102 for (year = year1; year <= year2; year++) {
1103 printf("Sun in %d:\n", year);
1104 fequinoxsolstice(year, UTCOffset, equinoxdays,
1106 printf("e[0] - %g (%s)\n",
1108 floattoday(year, equinoxdays[0]));
1109 printf("e[1] - %g (%s)\n",
1111 floattoday(year, equinoxdays[1]));
1112 printf("s[0] - %g (%s)\n",
1114 floattoday(year, solsticedays[0]));
1115 printf("s[1] - %g (%s)\n",
1117 floattoday(year, solsticedays[1]));