2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
31 __FBSDID("$FreeBSD$");
42 #define SLEN 100 /* maximum length of date spec. part strings */
44 static char *showflags(int flags);
45 static int isonlydigits(char *s, int nostar);
46 static const char *getmonthname(int i);
47 static int checkmonth(char *s, size_t *len, size_t *offset, const char **month);
48 static const char *getdayofweekname(int i);
49 static int checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow);
50 static int indextooffset(char *s);
51 static int parseoffset(char *s);
52 static char *floattoday(int year, double f);
53 static char *floattotime(double f);
54 static int wdayom (int day, int offset, int month, int year);
59 * Date ::= Month . ' ' . DayOfMonth |
60 * Month . ' ' . DayOfWeek . ModifierIndex |
61 * Month . '/' . DayOfMonth |
62 * Month . '/' . DayOfWeek . ModifierIndex |
63 * DayOfMonth . ' ' . Month |
64 * DayOfMonth . '/' . Month |
65 * DayOfWeek . ModifierIndex . ' ' .Month |
66 * DayOfWeek . ModifierIndex . '/' .Month |
67 * DayOfWeek . ModifierIndex |
68 * SpecialDay . ModifierOffset
70 * Month ::= MonthName | MonthNumber | '*'
71 * MonthNumber ::= '0' ... '9' | '00' ... '09' | '10' ... '12'
72 * MonthName ::= MonthNameShort | MonthNameLong
73 * MonthNameLong ::= 'January' ... 'December'
74 * MonthNameShort ::= 'Jan' ... 'Dec' | 'Jan.' ... 'Dec.'
76 * DayOfWeek ::= DayOfWeekShort | DayOfWeekLong
77 * DayOfWeekShort ::= 'Mon' .. 'Sun'
78 * DayOfWeekLong ::= 'Monday' .. 'Sunday'
79 * DayOfMonth ::= '0' ... '9' | '00' ... '09' | '10' ... '29' |
82 * ModifierOffset ::= '' | '+' . ModifierNumber | '-' . ModifierNumber
83 * ModifierNumber ::= '0' ... '9' | '00' ... '99' | '000' ... '299' |
84 * '300' ... '359' | '360' ... '365'
85 * ModifierIndex ::= 'Second' | 'Third' | 'Fourth' | 'Fifth' |
88 * SpecialDay ::= 'Easter' | 'Paskha' | 'ChineseNewYear'
92 determinestyle(char *date, int *flags,
93 char *month, int *imonth, char *dayofmonth, int *idayofmonth,
94 char *dayofweek, int *idayofweek, char *modifieroffset,
95 char *modifierindex, char *specialday, char *year, int *iyear)
97 char *p, *p1, *p2, *py;
98 const char *dow, *pmonth;
111 *modifieroffset = '\0';
112 *modifierindex = '\0';
115 #define CHECKSPECIAL(s1, s2, lens2, type) \
116 if (s2 != NULL && strncmp(s1, s2, lens2) == 0) { \
117 *flags |= F_SPECIALDAY; \
119 *flags |= F_VARIABLE; \
120 if (strlen(s1) == lens2) { \
121 strlcpy(specialday, s1, SLEN); \
124 strncpy(specialday, s1, lens2); \
125 specialday[lens2] = '\0'; \
126 strlcpy(modifieroffset, s1 + lens2, SLEN); \
127 *flags |= F_MODIFIEROFFSET; \
131 if ((p = strchr(date, ' ')) == NULL) {
132 if ((p = strchr(date, '/')) == NULL) {
133 CHECKSPECIAL(date, STRING_CNY, strlen(STRING_CNY),
135 CHECKSPECIAL(date, ncny.name, ncny.len, F_CNY);
136 CHECKSPECIAL(date, STRING_NEWMOON,
137 strlen(STRING_NEWMOON), F_NEWMOON);
138 CHECKSPECIAL(date, nnewmoon.name, nnewmoon.len,
140 CHECKSPECIAL(date, STRING_FULLMOON,
141 strlen(STRING_FULLMOON), F_FULLMOON);
142 CHECKSPECIAL(date, nfullmoon.name, nfullmoon.len,
144 CHECKSPECIAL(date, STRING_PASKHA,
145 strlen(STRING_PASKHA), F_PASKHA);
146 CHECKSPECIAL(date, npaskha.name, npaskha.len, F_PASKHA);
147 CHECKSPECIAL(date, STRING_EASTER,
148 strlen(STRING_EASTER), F_EASTER);
149 CHECKSPECIAL(date, neaster.name, neaster.len, F_EASTER);
150 CHECKSPECIAL(date, STRING_MAREQUINOX,
151 strlen(STRING_MAREQUINOX), F_MAREQUINOX);
152 CHECKSPECIAL(date, nmarequinox.name, nmarequinox.len,
154 CHECKSPECIAL(date, STRING_SEPEQUINOX,
155 strlen(STRING_SEPEQUINOX), F_SEPEQUINOX);
156 CHECKSPECIAL(date, nsepequinox.name, nsepequinox.len,
158 CHECKSPECIAL(date, STRING_JUNSOLSTICE,
159 strlen(STRING_JUNSOLSTICE), F_JUNSOLSTICE);
160 CHECKSPECIAL(date, njunsolstice.name, njunsolstice.len,
162 CHECKSPECIAL(date, STRING_DECSOLSTICE,
163 strlen(STRING_DECSOLSTICE), F_DECSOLSTICE);
164 CHECKSPECIAL(date, ndecsolstice.name, ndecsolstice.len,
166 if (checkdayofweek(date, &len, &offset, &dow) != 0) {
167 *flags |= F_DAYOFWEEK;
168 *flags |= F_VARIABLE;
169 *idayofweek = offset;
170 if (strlen(date) == len) {
171 strlcpy(dayofweek, date, SLEN);
174 strncpy(dayofweek, date, len);
175 dayofweek[len] = '\0';
176 strlcpy(modifierindex, date + len, SLEN);
177 *flags |= F_MODIFIERINDEX;
180 if (isonlydigits(date, 1)) {
181 /* Assume month number only */
183 *imonth = (int)strtol(date, (char **)NULL, 10);
184 strlcpy(month, getmonthname(*imonth), SLEN);
192 * After this, leave by goto-ing to "allfine" or "fail" to restore the
193 * original data in `date'.
199 /* Now p2 points to the next field and p1 to the first field */
201 if ((py = strchr(p2, '/')) != NULL) {
202 /* We have a year in the string. Now this is getting tricky */
203 strlcpy(year, p1, SLEN);
204 *iyear = (int)strtol(year, NULL, 10);
211 /* Check if there is a month-string in the date */
212 if ((checkmonth(p1, &len, &offset, &pmonth) != 0)
213 || (checkmonth(p2, &len, &offset, &pmonth) != 0 && (p2 = p1))) {
214 /* p2 is the non-month part */
218 strlcpy(month, getmonthname(offset), SLEN);
219 if (isonlydigits(p2, 1)) {
220 strlcpy(dayofmonth, p2, SLEN);
221 *idayofmonth = (int)strtol(p2, (char **)NULL, 10);
222 *flags |= F_DAYOFMONTH;
225 if (strcmp(p2, "*") == 0) {
230 if (checkdayofweek(p2, &len, &offset, &dow) != 0) {
231 *flags |= F_DAYOFWEEK;
232 *flags |= F_VARIABLE;
233 *idayofweek = offset;
234 strlcpy(dayofweek, getdayofweekname(offset), SLEN);
235 if (strlen(p2) == len)
237 strlcpy(modifierindex, p2 + len, SLEN);
238 *flags |= F_MODIFIERINDEX;
244 /* Check if there is an every-day or every-month in the string */
245 if ((strcmp(p1, "*") == 0 && isonlydigits(p2, 1))
246 || (strcmp(p2, "*") == 0 && isonlydigits(p1, 1) && (p2 = p1))) {
249 *flags |= F_ALLMONTH;
250 *flags |= F_DAYOFMONTH;
251 d = (int)strtol(p2, (char **)NULL, 10);
253 snprintf(dayofmonth, SLEN, "%d", d);
257 /* Month as a number, then a weekday */
258 if (isonlydigits(p1, 1)
259 && checkdayofweek(p2, &len, &offset, &dow) != 0) {
263 *flags |= F_DAYOFWEEK;
264 *flags |= F_VARIABLE;
266 *idayofweek = offset;
267 d = (int)strtol(p1, (char **)NULL, 10);
269 strlcpy(month, getmonthname(d), SLEN);
271 strlcpy(dayofweek, getdayofweekname(offset), SLEN);
272 if (strlen(p2) == len)
274 strlcpy(modifierindex, p2 + len, SLEN);
275 *flags |= F_MODIFIERINDEX;
279 /* If both the month and date are specified as numbers */
280 if (isonlydigits(p1, 1) && isonlydigits(p2, 0)) {
281 /* Now who wants to be this ambiguous? :-( */
284 if (strchr(p2, '*') != NULL)
285 *flags |= F_VARIABLE;
287 m = (int)strtol(p1, (char **)NULL, 10);
288 d = (int)strtol(p2, (char **)NULL, 10);
291 *flags |= F_DAYOFMONTH;
296 strlcpy(month, getmonthname(d), SLEN);
297 snprintf(dayofmonth, SLEN, "%d", m);
301 strlcpy(month, getmonthname(m), SLEN);
302 snprintf(dayofmonth, SLEN, "%d", d);
318 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
321 static int warned = 0;
323 if (*rememberindex >= MAXCOUNT - 1) {
325 warnx("Index > %d, ignored", MAXCOUNT);
329 y[*rememberindex] = yy;
330 m[*rememberindex] = mm;
331 d[*rememberindex] = dd;
333 strlcpy(ed[*rememberindex], extra, SLEN);
335 ed[*rememberindex][0] = '\0';
340 debug_determinestyle(int dateonly, char *date, int flags, char *month,
341 int imonth, char *dayofmonth, int idayofmonth, char *dayofweek,
342 int idayofweek, char *modifieroffset, char *modifierindex, char *specialday,
343 char *year, int iyear)
347 printf("-------\ndate: |%s|\n", date);
351 printf("flags: %x - %s\n", flags, showflags(flags));
352 if (modifieroffset[0] != '\0')
353 printf("modifieroffset: |%s|\n", modifieroffset);
354 if (modifierindex[0] != '\0')
355 printf("modifierindex: |%s|\n", modifierindex);
357 printf("year: |%s| (%d)\n", year, iyear);
358 if (month[0] != '\0')
359 printf("month: |%s| (%d)\n", month, imonth);
360 if (dayofmonth[0] != '\0')
361 printf("dayofmonth: |%s| (%d)\n", dayofmonth, idayofmonth);
362 if (dayofweek[0] != '\0')
363 printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
364 if (specialday[0] != '\0')
365 printf("specialday: |%s|\n", specialday);
368 static struct yearinfo {
370 int ieaster, ipaskha, firstcnyday;
371 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
372 double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
373 int ichinesemonths[MAXMOONS];
374 double equinoxdays[2], solsticedays[2];
376 struct yearinfo *next;
380 * Calculate dates with offset from weekdays, like Thurs-3, Wed+2, etc.
381 * day is the day of the week,
382 * offset the ordinal number of the weekday in the month.
385 wdayom (int day, int offset, int month, int year)
387 /* Weekday of first day in month */
388 int wday1; /* first day of month */
389 /* Weekday of last day in month */
393 wday1 = first_dayofweek_of_month(year, month);
394 if (wday1 < 0) /* not set */
397 * Date of zeroth or first of our weekday in month, depending on the
398 * relationship with the first of the month. The range is -6:6.
400 d = (day - wday1 + 1) % 7;
402 * Which way are we counting? Offset 0 is invalid, abs (offset) > 5 is
403 * meaningless, but that's OK. Offset 5 may or may not be meaningless,
404 * so there's no point in complaining for complaining's sake.
406 if (offset < 0) { /* back from end of month */
409 while (wdayn <= yearinfo->monthdays[month])
411 d = offset * 7 + wdayn;
412 } else if (offset > 0){
418 warnx ("Invalid offset 0");
423 * Possible date formats include any combination of:
424 * 3-charmonth (January, Jan, Jan)
425 * 3-charweekday (Friday, Monday, mon.)
426 * numeric month or day (1, 2, 04)
428 * Any character may separate them, or they may not be separated. Any line,
429 * following a line that is matched, that starts with "whitespace", is shown
430 * along with the matched line.
433 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
436 char month[SLEN], dayofmonth[SLEN], dayofweek[SLEN], modifieroffset[SLEN];
438 char modifierindex[SLEN], specialday[SLEN];
439 int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
441 int d, m, dow, rm, rd, offset;
449 * Monthname: Jan .. Dec
451 * Weekday: Mon .. Sun
458 debug_determinestyle(1, date, *flags, month, imonth,
459 dayofmonth, idayofmonth, dayofweek, idayofweek,
460 modifieroffset, modifierindex, specialday, syear, iyear);
461 if (determinestyle(date, flags, month, &imonth, dayofmonth,
462 &idayofmonth, dayofweek, &idayofweek, modifieroffset,
463 modifierindex, specialday, syear, &iyear) == 0) {
470 debug_determinestyle(0, date, *flags, month, imonth,
471 dayofmonth, idayofmonth, dayofweek, idayofweek,
472 modifieroffset, modifierindex, specialday, syear, iyear);
475 for (year = year1; year <= year2; year++) {
478 /* If the year is specified, only do it if it is this year! */
479 if ((lflags & F_YEAR) != 0)
484 /* Get important dates for this year */
486 while (yearinfo != NULL) {
487 if (yearinfo->year == year)
489 yearinfo = yearinfo -> next;
491 if (yearinfo == NULL) {
492 yearinfo = (struct yearinfo *)calloc(1,
493 sizeof(struct yearinfo));
494 if (yearinfo == NULL)
495 errx(1, "Unable to allocate more years");
496 yearinfo->year = year;
497 yearinfo->next = years;
500 yearinfo->monthdays = monthdaytab[isleap(year)];
501 yearinfo->ieaster = easter(year);
502 yearinfo->ipaskha = paskha(year);
503 fpom(year, UTCOffset, yearinfo->ffullmoon,
505 fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
506 yearinfo->fnewmooncny);
507 fequinoxsolstice(year, UTCOffset,
508 yearinfo->equinoxdays, yearinfo->solsticedays);
511 * CNY: Match day with sun longitude at 330` with new
514 yearinfo->firstcnyday = calculatesunlongitude30(year,
515 UTCOFFSET_CNY, yearinfo->ichinesemonths);
516 for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) {
517 if (yearinfo->fnewmooncny[m] >
518 yearinfo->firstcnyday) {
519 yearinfo->firstcnyday =
520 floor(yearinfo->fnewmooncny[m - 1]);
526 /* Same day every year */
527 if (lflags == (F_MONTH | F_DAYOFMONTH)) {
528 if (!remember_ymd(year, imonth, idayofmonth))
530 remember(&remindex, yearp, monthp, dayp, edp,
531 year, imonth, idayofmonth, NULL);
535 /* XXX Same day every year, but variable */
536 if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
537 if (!remember_ymd(year, imonth, idayofmonth))
539 remember(&remindex, yearp, monthp, dayp, edp,
540 year, imonth, idayofmonth, NULL);
544 /* Same day every month */
545 if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) {
546 for (m = 1; m <= 12; m++) {
547 if (!remember_ymd(year, m, idayofmonth))
549 remember(&remindex, yearp, monthp, dayp, edp,
550 year, m, idayofmonth, NULL);
555 /* Every day of a month */
556 if (lflags == (F_ALLDAY | F_MONTH)) {
557 for (d = 1; d <= yearinfo->monthdays[imonth]; d++) {
558 if (!remember_ymd(year, imonth, d))
560 remember(&remindex, yearp, monthp, dayp, edp,
561 year, imonth, d, NULL);
566 /* One day of every month */
567 if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) {
568 for (m = 1; m <= 12; m++) {
569 if (!remember_ymd(year, m, idayofmonth))
571 remember(&remindex, yearp, monthp, dayp, edp,
572 year, m, idayofmonth, NULL);
577 /* Every dayofweek of the year */
578 if (lflags == (F_DAYOFWEEK | F_VARIABLE)) {
579 dow = first_dayofweek_of_year(year);
582 d = (idayofweek - dow + 7) % 7 + 1;
584 if (remember_yd(year, d, &rm, &rd))
586 yearp, monthp, dayp, edp,
594 * Every so-manied dayofweek of every month of the year:
597 if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
598 offset = indextooffset(modifierindex);
600 for (m = 0; m <= 12; m++) {
601 d = wdayom (idayofweek, offset, m, year);
602 if (remember_ymd(year, m, d)) {
604 yearp, monthp, dayp, edp,
613 * A certain dayofweek of a month
617 (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
618 offset = indextooffset(modifierindex);
619 dow = first_dayofweek_of_month(year, imonth);
622 d = (idayofweek - dow + 7) % 7 + 1;
625 while (d <= yearinfo->monthdays[imonth]) {
627 && remember_ymd(year, imonth, d)) {
629 yearp, monthp, dayp, edp,
630 year, imonth, d, NULL);
638 while (d <= yearinfo->monthdays[imonth])
640 while (offset != 0) {
644 if (remember_ymd(year, imonth, d))
646 yearp, monthp, dayp, edp,
647 year, imonth, d, NULL);
653 /* Every dayofweek of the month */
654 if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
655 dow = first_dayofweek_of_month(year, imonth);
658 d = (idayofweek - dow + 7) % 7 + 1;
659 while (d <= yearinfo->monthdays[imonth]) {
660 if (remember_ymd(year, imonth, d))
662 yearp, monthp, dayp, edp,
663 year, imonth, d, NULL);
670 if ((lflags & ~F_MODIFIEROFFSET) ==
671 (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
673 if ((lflags & F_MODIFIEROFFSET) != 0)
674 offset = parseoffset(modifieroffset);
675 if (remember_yd(year, yearinfo->ieaster + offset,
677 remember(&remindex, yearp, monthp, dayp, edp,
683 if ((lflags & ~F_MODIFIEROFFSET) ==
684 (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
686 if ((lflags & F_MODIFIEROFFSET) != 0)
687 offset = parseoffset(modifieroffset);
688 if (remember_yd(year, yearinfo->ipaskha + offset,
690 remember(&remindex, yearp, monthp, dayp, edp,
695 /* Chinese New Year */
696 if ((lflags & ~F_MODIFIEROFFSET) ==
697 (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
699 if ((lflags & F_MODIFIEROFFSET) != 0)
700 offset = parseoffset(modifieroffset);
701 if (remember_yd(year, yearinfo->firstcnyday + offset,
703 remember(&remindex, yearp, monthp, dayp, edp,
709 if ((lflags & ~F_MODIFIEROFFSET) ==
710 (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
714 if ((lflags & F_MODIFIEROFFSET) != 0)
715 offset = parseoffset(modifieroffset);
716 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
717 if (remember_yd(year,
718 floor(yearinfo->ffullmoon[i]) + offset,
721 yearinfo->ffullmoon[i]);
723 yearp, monthp, dayp, edp,
731 if ((lflags & ~F_MODIFIEROFFSET) ==
732 (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
736 if ((lflags & F_MODIFIEROFFSET) != 0)
737 offset = parseoffset(modifieroffset);
738 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
739 if (remember_yd(year,
740 floor(yearinfo->fnewmoon[i]) + offset,
742 ed = floattotime(yearinfo->fnewmoon[i]);
744 yearp, monthp, dayp, edp,
751 /* (Mar|Sep)Equinox */
752 if ((lflags & ~F_MODIFIEROFFSET) ==
753 (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
755 if ((lflags & F_MODIFIEROFFSET) != 0)
756 offset = parseoffset(modifieroffset);
757 if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
759 ed = floattotime(yearinfo->equinoxdays[0]);
760 remember(&remindex, yearp, monthp, dayp, edp,
765 if ((lflags & ~F_MODIFIEROFFSET) ==
766 (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
768 if ((lflags & F_MODIFIEROFFSET) != 0)
769 offset = parseoffset(modifieroffset);
770 if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
772 ed = floattotime(yearinfo->equinoxdays[1]);
773 remember(&remindex, yearp, monthp, dayp, edp,
779 /* (Jun|Dec)Solstice */
780 if ((lflags & ~F_MODIFIEROFFSET) ==
781 (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
783 if ((lflags & F_MODIFIEROFFSET) != 0)
784 offset = parseoffset(modifieroffset);
785 if (remember_yd(year,
786 yearinfo->solsticedays[0] + offset, &rm, &rd)) {
787 ed = floattotime(yearinfo->solsticedays[0]);
788 remember(&remindex, yearp, monthp, dayp, edp,
793 if ((lflags & ~F_MODIFIEROFFSET) ==
794 (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
796 if ((lflags & F_MODIFIEROFFSET) != 0)
797 offset = parseoffset(modifieroffset);
798 if (remember_yd(year,
799 yearinfo->solsticedays[1] + offset, &rm, &rd)) {
800 ed = floattotime(yearinfo->solsticedays[1]);
801 remember(&remindex, yearp, monthp, dayp, edp,
808 printf("Unprocessed:\n");
809 debug_determinestyle(2, date, lflags, month, imonth,
810 dayofmonth, idayofmonth, dayofweek, idayofweek,
811 modifieroffset, modifierindex, specialday, syear,
817 if (retvalsign == -1)
818 return (-remindex - 1);
829 if ((flags & F_YEAR) != 0)
830 strlcat(s, "year ", SLEN);
831 if ((flags & F_MONTH) != 0)
832 strlcat(s, "month ", SLEN);
833 if ((flags & F_DAYOFWEEK) != 0)
834 strlcat(s, "dayofweek ", SLEN);
835 if ((flags & F_DAYOFMONTH) != 0)
836 strlcat(s, "dayofmonth ", SLEN);
837 if ((flags & F_MODIFIERINDEX) != 0)
838 strlcat(s, "modifierindex ", SLEN);
839 if ((flags & F_MODIFIEROFFSET) != 0)
840 strlcat(s, "modifieroffset ", SLEN);
841 if ((flags & F_SPECIALDAY) != 0)
842 strlcat(s, "specialday ", SLEN);
843 if ((flags & F_ALLMONTH) != 0)
844 strlcat(s, "allmonth ", SLEN);
845 if ((flags & F_ALLDAY) != 0)
846 strlcat(s, "allday ", SLEN);
847 if ((flags & F_VARIABLE) != 0)
848 strlcat(s, "variable ", SLEN);
849 if ((flags & F_CNY) != 0)
850 strlcat(s, "chinesenewyear ", SLEN);
851 if ((flags & F_PASKHA) != 0)
852 strlcat(s, "paskha ", SLEN);
853 if ((flags & F_EASTER) != 0)
854 strlcat(s, "easter ", SLEN);
855 if ((flags & F_FULLMOON) != 0)
856 strlcat(s, "fullmoon ", SLEN);
857 if ((flags & F_NEWMOON) != 0)
858 strlcat(s, "newmoon ", SLEN);
859 if ((flags & F_MAREQUINOX) != 0)
860 strlcat(s, "marequinox ", SLEN);
861 if ((flags & F_SEPEQUINOX) != 0)
862 strlcat(s, "sepequinox ", SLEN);
863 if ((flags & F_JUNSOLSTICE) != 0)
864 strlcat(s, "junsolstice ", SLEN);
865 if ((flags & F_DECSOLSTICE) != 0)
866 strlcat(s, "decsolstice ", SLEN);
874 if (i <= 0 || i > 12)
876 if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
877 return (nmonths[i - 1].name);
878 return (months[i - 1]);
882 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
887 for (i = 0; fnmonths[i].name != NULL; i++) {
889 if (strncasecmp(s, n->name, n->len) == 0) {
896 for (i = 0; nmonths[i].name != NULL; i++) {
898 if (strncasecmp(s, n->name, n->len) == 0) {
905 for (i = 0; fmonths[i] != NULL; i++) {
906 *len = strlen(fmonths[i]);
907 if (strncasecmp(s, fmonths[i], *len) == 0) {
913 for (i = 0; months[i] != NULL; i++) {
914 if (strncasecmp(s, months[i], 3) == 0) {
925 getdayofweekname(int i)
927 if (ndays[i].len != 0 && ndays[i].name != NULL)
928 return (ndays[i].name);
933 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
938 for (i = 0; fndays[i].name != NULL; i++) {
940 if (strncasecmp(s, n->name, n->len) == 0) {
947 for (i = 0; ndays[i].name != NULL; i++) {
949 if (strncasecmp(s, n->name, n->len) == 0) {
956 for (i = 0; fdays[i] != NULL; i++) {
957 *len = strlen(fdays[i]);
958 if (strncasecmp(s, fdays[i], *len) == 0) {
964 for (i = 0; days[i] != NULL; i++) {
965 if (strncasecmp(s, days[i], 3) == 0) {
976 isonlydigits(char *s, int nostar)
979 for (i = 0; s[i] != '\0'; i++) {
980 if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
982 if (!isdigit((unsigned char)s[i]))
989 indextooffset(char *s)
995 if (s[0] == '+' || s[0] == '-') {
996 i = strtol (s, &es, 10);
997 if (*es != '\0') /* trailing junk */
998 errx (1, "Invalid specifier format: %s\n", s);
1002 for (i = 0; i < 6; i++) {
1003 if (strcasecmp(s, sequences[i]) == 0) {
1009 for (i = 0; i < 6; i++) {
1013 if (strncasecmp(s, n->name, n->len) == 0) {
1023 parseoffset(char *s)
1025 return strtol(s, NULL, 10);
1029 floattotime(double f)
1031 static char buf[SLEN];
1037 hh = i / SECSPERHOUR;
1039 mm = i / SECSPERMINUTE;
1043 snprintf(buf, SLEN, "%02d:%02d:%02d", hh, mm, ss);
1048 floattoday(int year, double f)
1050 static char buf[SLEN];
1051 int i, m, d, hh, mm, ss;
1052 int *cumdays = cumdaytab[isleap(year)];
1054 for (i = 0; 1 + cumdays[i] < f; i++)
1057 d = floor(f - 1 - cumdays[i]);
1061 hh = i / SECSPERHOUR;
1063 mm = i / SECSPERMINUTE;
1067 snprintf(buf, SLEN, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
1076 printf("UTCOffset: %g\n", UTCOffset);
1077 printf("eastlongitude: %d\n", EastLongitude);
1079 if (strcmp(what, "moon") == 0) {
1080 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
1083 for (year = year1; year <= year2; year++) {
1084 fpom(year, UTCOffset, ffullmoon, fnewmoon);
1085 printf("Full moon %d:\t", year);
1086 for (i = 0; ffullmoon[i] >= 0; i++) {
1087 printf("%g (%s) ", ffullmoon[i],
1088 floattoday(year, ffullmoon[i]));
1090 printf("\nNew moon %d:\t", year);
1091 for (i = 0; fnewmoon[i] >= 0; i++) {
1092 printf("%g (%s) ", fnewmoon[i],
1093 floattoday(year, fnewmoon[i]));
1102 if (strcmp(what, "sun") == 0) {
1103 double equinoxdays[2], solsticedays[2];
1104 for (year = year1; year <= year2; year++) {
1105 printf("Sun in %d:\n", year);
1106 fequinoxsolstice(year, UTCOffset, equinoxdays,
1108 printf("e[0] - %g (%s)\n",
1110 floattoday(year, equinoxdays[0]));
1111 printf("e[1] - %g (%s)\n",
1113 floattoday(year, equinoxdays[1]));
1114 printf("s[0] - %g (%s)\n",
1116 floattoday(year, solsticedays[0]));
1117 printf("s[1] - %g (%s)\n",
1119 floattoday(year, solsticedays[1]));