2 * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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);
54 * Date ::= Month . ' ' . DayOfMonth |
55 * Month . ' ' . DayOfWeek . ModifierIndex |
56 * Month . '/' . DayOfMonth |
57 * Month . '/' . DayOfWeek . ModifierIndex |
58 * DayOfMonth . ' ' . Month |
59 * DayOfMonth . '/' . Month |
60 * DayOfWeek . ModifierIndex . ' ' .Month |
61 * DayOfWeek . ModifierIndex . '/' .Month |
62 * DayOfWeek . ModifierIndex |
63 * SpecialDay . ModifierOffset
65 * Month ::= MonthName | MonthNumber | '*'
66 * MonthNumber ::= '0' ... '9' | '00' ... '09' | '10' ... '12'
67 * MonthName ::= MonthNameShort | MonthNameLong
68 * MonthNameLong ::= 'January' ... 'December'
69 * MonthNameShort ::= 'Jan' ... 'Dec' | 'Jan.' ... 'Dec.'
71 * DayOfWeek ::= DayOfWeekShort | DayOfWeekLong
72 * DayOfWeekShort ::= 'Mon' .. 'Sun'
73 * DayOfWeekLong ::= 'Monday' .. 'Sunday'
74 * DayOfMonth ::= '0' ... '9' | '00' ... '09' | '10' ... '29' |
77 * ModifierOffset ::= '' | '+' . ModifierNumber | '-' . ModifierNumber
78 * ModifierNumber ::= '0' ... '9' | '00' ... '99' | '000' ... '299' |
79 * '300' ... '359' | '360' ... '365'
80 * ModifierIndex ::= 'Second' | 'Third' | 'Fourth' | 'Fifth' |
83 * SpecialDay ::= 'Easter' | 'Pashka' | 'ChineseNewYear'
87 determinestyle(char *date, int *flags,
88 char *month, int *imonth, char *dayofmonth, int *idayofmonth,
89 char *dayofweek, int *idayofweek, char *modifieroffset,
90 char *modifierindex, char *specialday, char *year, int *iyear)
92 char *p, *p1, *p2, *py;
93 const char *dow, *pmonth;
106 *modifieroffset = '\0';
107 *modifierindex = '\0';
110 #define CHECKSPECIAL(s1, s2, lens2, type) \
111 if (s2 != NULL && strncmp(s1, s2, lens2) == 0) { \
112 *flags |= F_SPECIALDAY; \
114 *flags |= F_VARIABLE; \
115 if (strlen(s1) == lens2) { \
116 strcpy(specialday, s1); \
119 strncpy(specialday, s1, lens2); \
120 specialday[lens2] = '\0'; \
121 strcpy(modifieroffset, s1 + lens2); \
122 *flags |= F_MODIFIEROFFSET; \
126 if ((p = strchr(date, ' ')) == NULL) {
127 if ((p = strchr(date, '/')) == NULL) {
128 CHECKSPECIAL(date, STRING_CNY, strlen(STRING_CNY),
130 CHECKSPECIAL(date, ncny.name, ncny.len, F_CNY);
131 CHECKSPECIAL(date, STRING_NEWMOON,
132 strlen(STRING_NEWMOON), F_NEWMOON);
133 CHECKSPECIAL(date, nnewmoon.name, nnewmoon.len,
135 CHECKSPECIAL(date, STRING_FULLMOON,
136 strlen(STRING_FULLMOON), F_FULLMOON);
137 CHECKSPECIAL(date, nfullmoon.name, nfullmoon.len,
139 CHECKSPECIAL(date, STRING_PASKHA,
140 strlen(STRING_PASKHA), F_PASKHA);
141 CHECKSPECIAL(date, npaskha.name, npaskha.len, F_PASKHA);
142 CHECKSPECIAL(date, STRING_EASTER,
143 strlen(STRING_EASTER), F_EASTER);
144 CHECKSPECIAL(date, neaster.name, neaster.len, F_EASTER);
145 CHECKSPECIAL(date, STRING_MAREQUINOX,
146 strlen(STRING_MAREQUINOX), F_MAREQUINOX);
147 CHECKSPECIAL(date, nmarequinox.name, nmarequinox.len,
149 CHECKSPECIAL(date, STRING_SEPEQUINOX,
150 strlen(STRING_SEPEQUINOX), F_SEPEQUINOX);
151 CHECKSPECIAL(date, nsepequinox.name, nsepequinox.len,
153 CHECKSPECIAL(date, STRING_JUNSOLSTICE,
154 strlen(STRING_JUNSOLSTICE), F_JUNSOLSTICE);
155 CHECKSPECIAL(date, njunsolstice.name, njunsolstice.len,
157 CHECKSPECIAL(date, STRING_DECSOLSTICE,
158 strlen(STRING_DECSOLSTICE), F_DECSOLSTICE);
159 CHECKSPECIAL(date, ndecsolstice.name, ndecsolstice.len,
161 if (checkdayofweek(date, &len, &offset, &dow) != 0) {
162 *flags |= F_DAYOFWEEK;
163 *flags |= F_VARIABLE;
164 *idayofweek = offset;
165 if (strlen(date) == len) {
166 strcpy(dayofweek, date);
169 strncpy(dayofweek, date, len);
170 dayofweek[len] = '\0';
171 strcpy(modifierindex, date + len);
172 *flags |= F_MODIFIERINDEX;
175 if (isonlydigits(date, 1)) {
176 /* Assume month number only */
178 *imonth = (int)strtol(date, (char **)NULL, 10);
179 strcpy(month, getmonthname(*imonth));
187 * AFTER this, leave by goto-ing to "allfine" or "fail" to restore the
188 * original data in `date'.
194 /* Now p2 points to the next field and p1 to the first field */
196 if ((py = strchr(p2, '/')) != NULL) {
197 /* We have a year in the string. Now this is getting tricky */
199 *iyear = (int)strtol(year, NULL, 10);
207 printf("p1: %s\n", p1);
208 printf("p2: %s\n", p2);
209 printf("year: %s\n", year);
212 /* Check if there is a month-string in the date */
213 if ((checkmonth(p1, &len, &offset, &pmonth) != 0)
214 || (checkmonth(p2, &len, &offset, &pmonth) != 0 && (p2 = p1))) {
215 /* p2 is the non-month part */
219 strcpy(month, getmonthname(offset));
220 if (isonlydigits(p2, 1)) {
221 strcpy(dayofmonth, p2);
222 *idayofmonth = (int)strtol(p2, (char **)NULL, 10);
223 *flags |= F_DAYOFMONTH;
226 if (strcmp(p2, "*") == 0) {
231 if (checkdayofweek(p2, &len, &offset, &dow) != 0) {
232 *flags |= F_DAYOFWEEK;
233 *flags |= F_VARIABLE;
234 *idayofweek = offset;
235 strcpy(dayofweek, getdayofweekname(offset));
236 if (strlen(p2) == len)
238 strcpy(modifierindex, p2 + len);
239 *flags |= F_MODIFIERINDEX;
246 /* Check if there is an every-day or every-month in the string */
247 if ((strcmp(p1, "*") == 0 && isonlydigits(p2, 1))
248 || (strcmp(p2, "*") == 0 && isonlydigits(p1, 1) && (p2 = p1))) {
251 *flags |= F_ALLMONTH;
252 *flags |= F_DAYOFMONTH;
253 d = (int)strtol(p2, (char **)NULL, 10);
255 sprintf(dayofmonth, "%d", d);
259 /* Month as a number, then a weekday */
260 if (isonlydigits(p1, 1)
261 && checkdayofweek(p2, &len, &offset, &dow) != 0) {
265 *flags |= F_DAYOFWEEK;
266 *flags |= F_VARIABLE;
268 *idayofweek = offset;
269 d = (int)strtol(p1, (char **)NULL, 10);
271 strcpy(month, getmonthname(d));
273 strcpy(dayofweek, getdayofweekname(offset));
274 if (strlen(p2) == len)
276 strcpy(modifierindex, p2 + len);
277 *flags |= F_MODIFIERINDEX;
281 /* If both the month and date are specified as numbers */
282 if (isonlydigits(p1, 1) && isonlydigits(p2, 0)) {
283 /* Now who wants to be this ambigious? :-( */
286 if (strchr(p2, '*') != NULL)
287 *flags |= F_VARIABLE;
289 m = (int)strtol(p1, (char **)NULL, 10);
290 d = (int)strtol(p2, (char **)NULL, 10);
293 *flags |= F_DAYOFMONTH;
298 strcpy(month, getmonthname(d));
299 sprintf(dayofmonth, "%d", m);
303 strcpy(month, getmonthname(m));
304 sprintf(dayofmonth, "%d", d);
320 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
323 static int warned = 0;
325 if (*rememberindex >= MAXCOUNT - 1) {
327 warnx("Index > %d, ignored", MAXCOUNT);
331 y[*rememberindex] = yy;
332 m[*rememberindex] = mm;
333 d[*rememberindex] = dd;
335 strcpy(ed[*rememberindex], extra);
337 ed[*rememberindex][0] = '\0';
342 debug_determinestyle(int dateonly, char *date, int flags, char *month,
343 int imonth, char *dayofmonth, int idayofmonth, char *dayofweek,
344 int idayofweek, char *modifieroffset, char *modifierindex, char *specialday,
345 char *year, int iyear)
349 printf("-------\ndate: |%s|\n", date);
353 printf("flags: %x - %s\n", flags, showflags(flags));
354 if (modifieroffset[0] != '\0')
355 printf("modifieroffset: |%s|\n", modifieroffset);
356 if (modifierindex[0] != '\0')
357 printf("modifierindex: |%s|\n", modifierindex);
359 printf("year: |%s| (%d)\n", year, iyear);
360 if (month[0] != '\0')
361 printf("month: |%s| (%d)\n", month, imonth);
362 if (dayofmonth[0] != '\0')
363 printf("dayofmonth: |%s| (%d)\n", dayofmonth, idayofmonth);
364 if (dayofweek[0] != '\0')
365 printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
366 if (specialday[0] != '\0')
367 printf("specialday: |%s|\n", specialday);
372 int ieaster, ipaskha, firstcnyday;
373 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
374 double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
375 int ichinesemonths[MAXMOONS];
376 double equinoxdays[2], solsticedays[2];
378 struct yearinfo *next;
381 * Possible date formats include any combination of:
382 * 3-charmonth (January, Jan, Jan)
383 * 3-charweekday (Friday, Monday, mon.)
384 * numeric month or day (1, 2, 04)
386 * Any character may separate them, or they may not be separated. Any line,
387 * following a line that is matched, that starts with "whitespace", is shown
388 * along with the matched line.
391 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
394 char month[100], dayofmonth[100], dayofweek[100], modifieroffset[100];
396 char modifierindex[100], specialday[100];
397 int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
399 int d, m, dow, rm, rd, offset;
403 static struct yearinfo *years, *yearinfo;
409 * Monthname: Jan .. Dec
411 * Weekday: Mon .. Sun
418 debug_determinestyle(1, date, *flags, month, imonth,
419 dayofmonth, idayofmonth, dayofweek, idayofweek,
420 modifieroffset, modifierindex, specialday, syear, iyear);
421 if (determinestyle(date, flags, month, &imonth, dayofmonth,
422 &idayofmonth, dayofweek, &idayofweek, modifieroffset,
423 modifierindex, specialday, syear, &iyear) == 0) {
430 debug_determinestyle(0, date, *flags, month, imonth,
431 dayofmonth, idayofmonth, dayofweek, idayofweek,
432 modifieroffset, modifierindex, specialday, syear, iyear);
435 for (year = year1; year <= year2; year++) {
438 /* If the year is specified, only do it if it is this year! */
439 if ((lflags & F_YEAR) != 0)
444 /* Get important dates for this year */
446 while (yearinfo != NULL) {
447 if (yearinfo->year == year)
449 yearinfo = yearinfo -> next;
451 if (yearinfo == NULL) {
452 yearinfo = (struct yearinfo *)calloc(1,
453 sizeof(struct yearinfo));
454 if (yearinfo == NULL)
455 errx(1, "Unable to allocate more years");
456 yearinfo->year = year;
457 yearinfo->next = years;
460 yearinfo->mondays = mondaytab[isleap(year)];
461 yearinfo->ieaster = easter(year);
462 fpom(year, UTCOffset, yearinfo->ffullmoon,
464 fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
465 yearinfo->fnewmooncny);
466 fequinoxsolstice(year, UTCOffset,
467 yearinfo->equinoxdays, yearinfo->solsticedays);
470 * CNY: Match day with sun longitude at 330` with new
473 yearinfo->firstcnyday = calculatesunlongitude30(year,
474 UTCOFFSET_CNY, yearinfo->ichinesemonths);
475 for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) {
476 if (yearinfo->fnewmooncny[m] >
477 yearinfo->firstcnyday) {
478 yearinfo->firstcnyday =
479 floor(yearinfo->fnewmooncny[m - 1]);
485 /* Same day every year */
486 if (lflags == (F_MONTH | F_DAYOFMONTH)) {
487 if (!remember_ymd(year, imonth, idayofmonth))
489 remember(&remindex, yearp, monthp, dayp, edp,
490 year, imonth, idayofmonth, NULL);
494 /* XXX Same day every year, but variable */
495 if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
496 if (!remember_ymd(year, imonth, idayofmonth))
498 remember(&remindex, yearp, monthp, dayp, edp,
499 year, imonth, idayofmonth, NULL);
503 /* Same day every month */
504 if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) {
505 for (m = 1; m <= 12; m++) {
506 if (!remember_ymd(year, m, idayofmonth))
508 remember(&remindex, yearp, monthp, dayp, edp,
509 year, m, idayofmonth, NULL);
514 /* Every day of a month */
515 if (lflags == (F_ALLDAY | F_MONTH)) {
516 for (d = 1; d <= yearinfo->mondays[imonth]; d++) {
517 if (!remember_ymd(year, imonth, d))
519 remember(&remindex, yearp, monthp, dayp, edp,
520 year, imonth, d, NULL);
525 /* One day of every month */
526 if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) {
527 for (m = 1; m <= 12; m++) {
528 if (!remember_ymd(year, m, idayofmonth))
530 remember(&remindex, yearp, monthp, dayp, edp,
531 year, m, idayofmonth, NULL);
536 /* Every dayofweek of the year */
537 if (lflags == (F_DAYOFWEEK | F_VARIABLE)) {
538 dow = first_dayofweek_of_year(year);
539 d = (idayofweek - dow + 8) % 7;
541 if (remember_yd(year, d, &rm, &rd))
543 yearp, monthp, dayp, edp,
550 /* A certain dayofweek of a month */
552 (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
553 offset = indextooffset(modifierindex);
554 dow = first_dayofweek_of_month(year, imonth);
555 d = (idayofweek - dow + 8) % 7;
558 while (d <= yearinfo->mondays[imonth]) {
560 && remember_ymd(year, imonth, d)) {
562 yearp, monthp, dayp, edp,
563 year, imonth, d, NULL);
571 while (d <= yearinfo->mondays[imonth])
573 while (offset != 0) {
577 if (remember_ymd(year, imonth, d))
579 yearp, monthp, dayp, edp,
580 year, imonth, d, NULL);
586 /* Every dayofweek of the month */
587 if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
588 dow = first_dayofweek_of_month(year, imonth);
589 d = (idayofweek - dow + 8) % 7;
590 while (d <= yearinfo->mondays[imonth]) {
591 if (remember_ymd(year, imonth, d))
593 yearp, monthp, dayp, edp,
594 year, imonth, d, NULL);
601 if ((lflags & ~F_MODIFIEROFFSET) ==
602 (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
604 if ((lflags & F_MODIFIEROFFSET) != 0)
605 offset = parseoffset(modifieroffset);
606 if (remember_yd(year, yearinfo->ieaster + offset,
608 remember(&remindex, yearp, monthp, dayp, edp,
614 if ((lflags & ~F_MODIFIEROFFSET) ==
615 (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
617 if ((lflags & F_MODIFIEROFFSET) != 0)
618 offset = parseoffset(modifieroffset);
619 if (remember_yd(year, yearinfo->ipaskha + offset,
621 remember(&remindex, yearp, monthp, dayp, edp,
626 /* Chinese New Year */
627 if ((lflags & ~F_MODIFIEROFFSET) ==
628 (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
630 if ((lflags & F_MODIFIEROFFSET) != 0)
631 offset = parseoffset(modifieroffset);
632 if (remember_yd(year, yearinfo->firstcnyday + offset,
634 remember(&remindex, yearp, monthp, dayp, edp,
640 if ((lflags & ~F_MODIFIEROFFSET) ==
641 (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
645 if ((lflags & F_MODIFIEROFFSET) != 0)
646 offset = parseoffset(modifieroffset);
647 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
648 if (remember_yd(year,
649 floor(yearinfo->ffullmoon[i]) + offset,
652 yearinfo->ffullmoon[i]);
654 yearp, monthp, dayp, edp,
662 if ((lflags & ~F_MODIFIEROFFSET) ==
663 (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
667 if ((lflags & F_MODIFIEROFFSET) != 0)
668 offset = parseoffset(modifieroffset);
669 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
670 if (remember_yd(year,
671 floor(yearinfo->fnewmoon[i]) + offset,
673 ed = floattotime(yearinfo->fnewmoon[i]);
675 yearp, monthp, dayp, edp,
682 /* (Mar|Sep)Equinox */
683 if ((lflags & ~F_MODIFIEROFFSET) ==
684 (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
686 if ((lflags & F_MODIFIEROFFSET) != 0)
687 offset = parseoffset(modifieroffset);
688 if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
690 ed = floattotime(yearinfo->equinoxdays[0]);
691 remember(&remindex, yearp, monthp, dayp, edp,
696 if ((lflags & ~F_MODIFIEROFFSET) ==
697 (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
699 if ((lflags & F_MODIFIEROFFSET) != 0)
700 offset = parseoffset(modifieroffset);
701 if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
703 ed = floattotime(yearinfo->equinoxdays[1]);
704 remember(&remindex, yearp, monthp, dayp, edp,
710 /* (Jun|Dec)Solstice */
711 if ((lflags & ~F_MODIFIEROFFSET) ==
712 (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
714 if ((lflags & F_MODIFIEROFFSET) != 0)
715 offset = parseoffset(modifieroffset);
716 if (remember_yd(year,
717 yearinfo->solsticedays[0] + offset, &rm, &rd)) {
718 ed = floattotime(yearinfo->solsticedays[0]);
719 remember(&remindex, yearp, monthp, dayp, edp,
724 if ((lflags & ~F_MODIFIEROFFSET) ==
725 (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
727 if ((lflags & F_MODIFIEROFFSET) != 0)
728 offset = parseoffset(modifieroffset);
729 if (remember_yd(year,
730 yearinfo->solsticedays[1] + offset, &rm, &rd)) {
731 ed = floattotime(yearinfo->solsticedays[1]);
732 remember(&remindex, yearp, monthp, dayp, edp,
738 printf("Unprocessed:\n");
739 debug_determinestyle(2, date, lflags, month, imonth,
740 dayofmonth, idayofmonth, dayofweek, idayofweek,
741 modifieroffset, modifierindex, specialday, syear, iyear);
745 if (retvalsign == -1)
746 return (-remindex - 1);
757 if ((flags & F_YEAR) != 0)
759 if ((flags & F_MONTH) != 0)
761 if ((flags & F_DAYOFWEEK) != 0)
762 strcat(s, "dayofweek ");
763 if ((flags & F_DAYOFMONTH) != 0)
764 strcat(s, "dayofmonth ");
765 if ((flags & F_MODIFIERINDEX) != 0)
766 strcat(s, "modifierindex ");
767 if ((flags & F_MODIFIEROFFSET) != 0)
768 strcat(s, "modifieroffset ");
769 if ((flags & F_SPECIALDAY) != 0)
770 strcat(s, "specialday ");
771 if ((flags & F_ALLMONTH) != 0)
772 strcat(s, "allmonth ");
773 if ((flags & F_ALLDAY) != 0)
774 strcat(s, "allday ");
775 if ((flags & F_VARIABLE) != 0)
776 strcat(s, "variable ");
777 if ((flags & F_CNY) != 0)
778 strcat(s, "chinesenewyear ");
779 if ((flags & F_PASKHA) != 0)
780 strcat(s, "paskha ");
781 if ((flags & F_EASTER) != 0)
782 strcat(s, "easter ");
783 if ((flags & F_FULLMOON) != 0)
784 strcat(s, "fullmoon ");
785 if ((flags & F_NEWMOON) != 0)
786 strcat(s, "newmoon ");
787 if ((flags & F_MAREQUINOX) != 0)
788 strcat(s, "marequinox ");
789 if ((flags & F_SEPEQUINOX) != 0)
790 strcat(s, "sepequinox ");
791 if ((flags & F_JUNSOLSTICE) != 0)
792 strcat(s, "junsolstice ");
793 if ((flags & F_DECSOLSTICE) != 0)
794 strcat(s, "decsolstice ");
802 if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
803 return (nmonths[i - 1].name);
804 return (months[i - 1]);
808 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
813 for (i = 0; fnmonths[i].name != NULL; i++) {
815 if (strncasecmp(s, n->name, n->len) == 0) {
822 for (i = 0; nmonths[i].name != NULL; i++) {
824 if (strncasecmp(s, n->name, n->len) == 0) {
831 for (i = 0; fmonths[i] != NULL; i++) {
832 *len = strlen(fmonths[i]);
833 if (strncasecmp(s, fmonths[i], *len) == 0) {
839 for (i = 0; months[i] != NULL; i++) {
840 if (strncasecmp(s, months[i], 3) == 0) {
851 getdayofweekname(int i)
853 if (ndays[i].len != 0 && ndays[i].name != NULL)
854 return (ndays[i].name);
859 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
864 for (i = 0; fndays[i].name != NULL; i++) {
866 if (strncasecmp(s, n->name, n->len) == 0) {
873 for (i = 0; ndays[i].name != NULL; i++) {
875 if (strncasecmp(s, n->name, n->len) == 0) {
882 for (i = 0; fdays[i] != NULL; i++) {
883 *len = strlen(fdays[i]);
884 if (strncasecmp(s, fdays[i], *len) == 0) {
890 for (i = 0; days[i] != NULL; i++) {
891 if (strncasecmp(s, days[i], 3) == 0) {
902 isonlydigits(char *s, int nostar)
905 for (i = 0; s[i] != '\0'; i++) {
906 if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
908 if (!isdigit((unsigned char)s[i]))
915 indextooffset(char *s)
920 for (i = 0; i < 6; i++) {
921 if (strcasecmp(s, sequences[i]) == 0) {
927 for (i = 0; i < 6; i++) {
931 if (strncasecmp(s, n->name, n->len) == 0) {
944 return strtol(s, NULL, 10);
948 floattotime(double f)
950 static char buf[100];
956 hh = i / SECSPERHOUR;
958 mm = i / SECSPERMINUTE;
962 sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
967 floattoday(int year, double f)
969 static char buf[100];
970 int i, m, d, hh, mm, ss;
971 int *cumdays = cumdaytab[isleap(year)];
973 for (i = 0; 1 + cumdays[i] < f; i++)
976 d = floor(f - 1 - cumdays[i]);
980 hh = i / SECSPERHOUR;
982 mm = i / SECSPERMINUTE;
986 sprintf(buf, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
995 printf("UTCOffset: %g\n", UTCOffset);
996 printf("eastlongitude: %d\n", EastLongitude);
998 if (strcmp(what, "moon") == 0) {
999 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
1002 for (year = year1; year <= year2; year++) {
1003 fpom(year, UTCOffset, ffullmoon, fnewmoon);
1004 printf("Full moon %d:\t", year);
1005 for (i = 0; ffullmoon[i] >= 0; i++) {
1006 printf("%g (%s) ", ffullmoon[i],
1007 floattoday(year, ffullmoon[i]));
1009 printf("\nNew moon %d:\t", year);
1010 for (i = 0; fnewmoon[i] >= 0; i++) {
1011 printf("%g (%s) ", fnewmoon[i],
1012 floattoday(year, fnewmoon[i]));
1021 if (strcmp(what, "sun") == 0) {
1022 double equinoxdays[2], solsticedays[2];
1023 for (year = year1; year <= year2; year++) {
1024 printf("Sun in %d:\n", year);
1025 fequinoxsolstice(year, UTCOffset, equinoxdays,
1027 printf("e[0] - %g (%s)\n",
1029 floattoday(year, equinoxdays[0]));
1030 printf("e[1] - %g (%s)\n",
1032 floattoday(year, equinoxdays[1]));
1033 printf("s[0] - %g (%s)\n",
1035 floattoday(year, solsticedays[0]));
1036 printf("s[1] - %g (%s)\n",
1038 floattoday(year, solsticedays[1]));