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);
50 static int wdayom (int day, int offset, int month, int year);
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
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.'
72 * DayOfWeek ::= DayOfWeekShort | DayOfWeekLong
73 * DayOfWeekShort ::= 'Mon' .. 'Sun'
74 * DayOfWeekLong ::= 'Monday' .. 'Sunday'
75 * DayOfMonth ::= '0' ... '9' | '00' ... '09' | '10' ... '29' |
78 * ModifierOffset ::= '' | '+' . ModifierNumber | '-' . ModifierNumber
79 * ModifierNumber ::= '0' ... '9' | '00' ... '99' | '000' ... '299' |
80 * '300' ... '359' | '360' ... '365'
81 * ModifierIndex ::= 'Second' | 'Third' | 'Fourth' | 'Fifth' |
84 * SpecialDay ::= 'Easter' | 'Paskha' | 'ChineseNewYear'
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)
93 char *p, *p1, *p2, *py;
94 const char *dow, *pmonth;
107 *modifieroffset = '\0';
108 *modifierindex = '\0';
111 #define CHECKSPECIAL(s1, s2, lens2, type) \
112 if (s2 != NULL && strncmp(s1, s2, lens2) == 0) { \
113 *flags |= F_SPECIALDAY; \
115 *flags |= F_VARIABLE; \
116 if (strlen(s1) == lens2) { \
117 strcpy(specialday, s1); \
120 strncpy(specialday, s1, lens2); \
121 specialday[lens2] = '\0'; \
122 strcpy(modifieroffset, s1 + lens2); \
123 *flags |= F_MODIFIEROFFSET; \
127 if ((p = strchr(date, ' ')) == NULL) {
128 if ((p = strchr(date, '/')) == NULL) {
129 CHECKSPECIAL(date, STRING_CNY, strlen(STRING_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,
136 CHECKSPECIAL(date, STRING_FULLMOON,
137 strlen(STRING_FULLMOON), F_FULLMOON);
138 CHECKSPECIAL(date, nfullmoon.name, nfullmoon.len,
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,
150 CHECKSPECIAL(date, STRING_SEPEQUINOX,
151 strlen(STRING_SEPEQUINOX), F_SEPEQUINOX);
152 CHECKSPECIAL(date, nsepequinox.name, nsepequinox.len,
154 CHECKSPECIAL(date, STRING_JUNSOLSTICE,
155 strlen(STRING_JUNSOLSTICE), F_JUNSOLSTICE);
156 CHECKSPECIAL(date, njunsolstice.name, njunsolstice.len,
158 CHECKSPECIAL(date, STRING_DECSOLSTICE,
159 strlen(STRING_DECSOLSTICE), F_DECSOLSTICE);
160 CHECKSPECIAL(date, ndecsolstice.name, ndecsolstice.len,
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);
170 strncpy(dayofweek, date, len);
171 dayofweek[len] = '\0';
172 strcpy(modifierindex, date + len);
173 *flags |= F_MODIFIERINDEX;
176 if (isonlydigits(date, 1)) {
177 /* Assume month number only */
179 *imonth = (int)strtol(date, (char **)NULL, 10);
180 strcpy(month, getmonthname(*imonth));
188 * After this, leave by goto-ing to "allfine" or "fail" to restore the
189 * original data in `date'.
195 /* Now p2 points to the next field and p1 to the first field */
197 if ((py = strchr(p2, '/')) != NULL) {
198 /* We have a year in the string. Now this is getting tricky */
200 *iyear = (int)strtol(year, NULL, 10);
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 */
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;
221 if (strcmp(p2, "*") == 0) {
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)
233 strcpy(modifierindex, p2 + len);
234 *flags |= F_MODIFIERINDEX;
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))) {
245 *flags |= F_ALLMONTH;
246 *flags |= F_DAYOFMONTH;
247 d = (int)strtol(p2, (char **)NULL, 10);
249 sprintf(dayofmonth, "%d", d);
253 /* Month as a number, then a weekday */
254 if (isonlydigits(p1, 1)
255 && checkdayofweek(p2, &len, &offset, &dow) != 0) {
259 *flags |= F_DAYOFWEEK;
260 *flags |= F_VARIABLE;
262 *idayofweek = offset;
263 d = (int)strtol(p1, (char **)NULL, 10);
265 strcpy(month, getmonthname(d));
267 strcpy(dayofweek, getdayofweekname(offset));
268 if (strlen(p2) == len)
270 strcpy(modifierindex, p2 + len);
271 *flags |= F_MODIFIERINDEX;
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? :-( */
280 if (strchr(p2, '*') != NULL)
281 *flags |= F_VARIABLE;
283 m = (int)strtol(p1, (char **)NULL, 10);
284 d = (int)strtol(p2, (char **)NULL, 10);
287 *flags |= F_DAYOFMONTH;
292 strcpy(month, getmonthname(d));
293 sprintf(dayofmonth, "%d", m);
297 strcpy(month, getmonthname(m));
298 sprintf(dayofmonth, "%d", d);
314 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
315 int dd, char *extra);
317 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
320 static int warned = 0;
322 if (*rememberindex >= MAXCOUNT - 1) {
324 warnx("Index > %d, ignored", MAXCOUNT);
328 y[*rememberindex] = yy;
329 m[*rememberindex] = mm;
330 d[*rememberindex] = dd;
332 strcpy(ed[*rememberindex], extra);
334 ed[*rememberindex][0] = '\0';
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)
346 printf("-------\ndate: |%s|\n", date);
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);
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);
367 static struct yearinfo {
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];
375 struct yearinfo *next;
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.
384 wdayom (int day, int offset, int month, int year)
386 /* Weekday of first day in month */
387 int wday1; /* first day of month */
388 /* Weekday of last day in month */
392 wday1 = first_dayofweek_of_month(year, month);
393 if (wday1 < 0) /* not set */
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.
399 d = (day - wday1 + 1) % 7;
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.
405 if (offset < 0) { /* back from end of month */
408 while (wdayn <= yearinfo->monthdays[month])
410 d = offset * 7 + wdayn;
411 } else if (offset > 0){
417 warnx ("Invalid offset 0");
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)
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.
432 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
435 char month[100], dayofmonth[100], dayofweek[100], modifieroffset[100];
437 char modifierindex[100], specialday[100];
438 int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
440 int d, m, dow, rm, rd, offset;
448 * Monthname: Jan .. Dec
450 * Weekday: Mon .. Sun
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) {
469 debug_determinestyle(0, date, *flags, month, imonth,
470 dayofmonth, idayofmonth, dayofweek, idayofweek,
471 modifieroffset, modifierindex, specialday, syear, iyear);
474 for (year = year1; year <= year2; year++) {
477 /* If the year is specified, only do it if it is this year! */
478 if ((lflags & F_YEAR) != 0)
483 /* Get important dates for this year */
485 while (yearinfo != NULL) {
486 if (yearinfo->year == year)
488 yearinfo = yearinfo -> next;
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;
499 yearinfo->monthdays = monthdaytab[isleap(year)];
500 yearinfo->ieaster = easter(year);
501 yearinfo->ipaskha = paskha(year);
502 fpom(year, UTCOffset, yearinfo->ffullmoon,
504 fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
505 yearinfo->fnewmooncny);
506 fequinoxsolstice(year, UTCOffset,
507 yearinfo->equinoxdays, yearinfo->solsticedays);
510 * CNY: Match day with sun longitude at 330` with new
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]);
525 /* Same day every year */
526 if (lflags == (F_MONTH | F_DAYOFMONTH)) {
527 if (!remember_ymd(year, imonth, idayofmonth))
529 remember(&remindex, yearp, monthp, dayp, edp,
530 year, imonth, idayofmonth, NULL);
534 /* XXX Same day every year, but variable */
535 if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
536 if (!remember_ymd(year, imonth, idayofmonth))
538 remember(&remindex, yearp, monthp, dayp, edp,
539 year, imonth, idayofmonth, NULL);
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))
548 remember(&remindex, yearp, monthp, dayp, edp,
549 year, m, idayofmonth, NULL);
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))
559 remember(&remindex, yearp, monthp, dayp, edp,
560 year, imonth, d, NULL);
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))
570 remember(&remindex, yearp, monthp, dayp, edp,
571 year, m, idayofmonth, NULL);
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;
581 if (remember_yd(year, d, &rm, &rd))
583 yearp, monthp, dayp, edp,
591 * Every so-manied dayofweek of every month of the year:
594 if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
595 offset = indextooffset(modifierindex);
597 for (m = 0; m <= 12; m++) {
598 d = wdayom (idayofweek, offset, m, year);
599 if (remember_ymd(year, m, d)) {
601 yearp, monthp, dayp, edp,
610 * A certain dayofweek of a month
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;
620 while (d <= yearinfo->monthdays[imonth]) {
622 && remember_ymd(year, imonth, d)) {
624 yearp, monthp, dayp, edp,
625 year, imonth, d, NULL);
633 while (d <= yearinfo->monthdays[imonth])
635 while (offset != 0) {
639 if (remember_ymd(year, imonth, d))
641 yearp, monthp, dayp, edp,
642 year, imonth, d, NULL);
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))
655 yearp, monthp, dayp, edp,
656 year, imonth, d, NULL);
663 if ((lflags & ~F_MODIFIEROFFSET) ==
664 (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
666 if ((lflags & F_MODIFIEROFFSET) != 0)
667 offset = parseoffset(modifieroffset);
668 if (remember_yd(year, yearinfo->ieaster + offset,
670 remember(&remindex, yearp, monthp, dayp, edp,
676 if ((lflags & ~F_MODIFIEROFFSET) ==
677 (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
679 if ((lflags & F_MODIFIEROFFSET) != 0)
680 offset = parseoffset(modifieroffset);
681 if (remember_yd(year, yearinfo->ipaskha + offset,
683 remember(&remindex, yearp, monthp, dayp, edp,
688 /* Chinese New Year */
689 if ((lflags & ~F_MODIFIEROFFSET) ==
690 (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
692 if ((lflags & F_MODIFIEROFFSET) != 0)
693 offset = parseoffset(modifieroffset);
694 if (remember_yd(year, yearinfo->firstcnyday + offset,
696 remember(&remindex, yearp, monthp, dayp, edp,
702 if ((lflags & ~F_MODIFIEROFFSET) ==
703 (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
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,
714 yearinfo->ffullmoon[i]);
716 yearp, monthp, dayp, edp,
724 if ((lflags & ~F_MODIFIEROFFSET) ==
725 (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
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,
735 ed = floattotime(yearinfo->fnewmoon[i]);
737 yearp, monthp, dayp, edp,
744 /* (Mar|Sep)Equinox */
745 if ((lflags & ~F_MODIFIEROFFSET) ==
746 (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
748 if ((lflags & F_MODIFIEROFFSET) != 0)
749 offset = parseoffset(modifieroffset);
750 if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
752 ed = floattotime(yearinfo->equinoxdays[0]);
753 remember(&remindex, yearp, monthp, dayp, edp,
758 if ((lflags & ~F_MODIFIEROFFSET) ==
759 (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
761 if ((lflags & F_MODIFIEROFFSET) != 0)
762 offset = parseoffset(modifieroffset);
763 if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
765 ed = floattotime(yearinfo->equinoxdays[1]);
766 remember(&remindex, yearp, monthp, dayp, edp,
772 /* (Jun|Dec)Solstice */
773 if ((lflags & ~F_MODIFIEROFFSET) ==
774 (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
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,
786 if ((lflags & ~F_MODIFIEROFFSET) ==
787 (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
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,
801 printf("Unprocessed:\n");
802 debug_determinestyle(2, date, lflags, month, imonth,
803 dayofmonth, idayofmonth, dayofweek, idayofweek,
804 modifieroffset, modifierindex, specialday, syear,
810 if (retvalsign == -1)
811 return (-remindex - 1);
822 if ((flags & F_YEAR) != 0)
824 if ((flags & F_MONTH) != 0)
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 ");
867 if (i <= 0 || i > 12)
869 if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
870 return (nmonths[i - 1].name);
871 return (months[i - 1]);
875 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
880 for (i = 0; fnmonths[i].name != NULL; i++) {
882 if (strncasecmp(s, n->name, n->len) == 0) {
889 for (i = 0; nmonths[i].name != NULL; i++) {
891 if (strncasecmp(s, n->name, n->len) == 0) {
898 for (i = 0; fmonths[i] != NULL; i++) {
899 *len = strlen(fmonths[i]);
900 if (strncasecmp(s, fmonths[i], *len) == 0) {
906 for (i = 0; months[i] != NULL; i++) {
907 if (strncasecmp(s, months[i], 3) == 0) {
918 getdayofweekname(int i)
920 if (ndays[i].len != 0 && ndays[i].name != NULL)
921 return (ndays[i].name);
926 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
931 for (i = 0; fndays[i].name != NULL; i++) {
933 if (strncasecmp(s, n->name, n->len) == 0) {
940 for (i = 0; ndays[i].name != NULL; i++) {
942 if (strncasecmp(s, n->name, n->len) == 0) {
949 for (i = 0; fdays[i] != NULL; i++) {
950 *len = strlen(fdays[i]);
951 if (strncasecmp(s, fdays[i], *len) == 0) {
957 for (i = 0; days[i] != NULL; i++) {
958 if (strncasecmp(s, days[i], 3) == 0) {
969 isonlydigits(char *s, int nostar)
972 for (i = 0; s[i] != '\0'; i++) {
973 if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
975 if (!isdigit((unsigned char)s[i]))
982 indextooffset(char *s)
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);
995 for (i = 0; i < 6; i++) {
996 if (strcasecmp(s, sequences[i]) == 0) {
1002 for (i = 0; i < 6; i++) {
1006 if (strncasecmp(s, n->name, n->len) == 0) {
1016 parseoffset(char *s)
1018 return strtol(s, NULL, 10);
1022 floattotime(double f)
1024 static char buf[100];
1030 hh = i / SECSPERHOUR;
1032 mm = i / SECSPERMINUTE;
1036 sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
1041 floattoday(int year, double f)
1043 static char buf[100];
1044 int i, m, d, hh, mm, ss;
1045 int *cumdays = cumdaytab[isleap(year)];
1047 for (i = 0; 1 + cumdays[i] < f; i++)
1050 d = floor(f - 1 - cumdays[i]);
1054 hh = i / SECSPERHOUR;
1056 mm = i / SECSPERMINUTE;
1060 sprintf(buf, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
1069 printf("UTCOffset: %g\n", UTCOffset);
1070 printf("eastlongitude: %d\n", EastLongitude);
1072 if (strcmp(what, "moon") == 0) {
1073 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
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]));
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]));
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,
1101 printf("e[0] - %g (%s)\n",
1103 floattoday(year, equinoxdays[0]));
1104 printf("e[1] - %g (%s)\n",
1106 floattoday(year, equinoxdays[1]));
1107 printf("s[0] - %g (%s)\n",
1109 floattoday(year, solsticedays[0]));
1110 printf("s[1] - %g (%s)\n",
1112 floattoday(year, solsticedays[1]));