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 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 strcpy(specialday, s1); \
122 strncpy(specialday, s1, lens2); \
123 specialday[lens2] = '\0'; \
124 strcpy(modifieroffset, s1 + lens2); \
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 strcpy(dayofweek, date);
172 strncpy(dayofweek, date, len);
173 dayofweek[len] = '\0';
174 strcpy(modifierindex, date + len);
175 *flags |= F_MODIFIERINDEX;
178 if (isonlydigits(date, 1)) {
179 /* Assume month number only */
181 *imonth = (int)strtol(date, (char **)NULL, 10);
182 strcpy(month, getmonthname(*imonth));
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 */
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 strcpy(month, getmonthname(offset));
217 if (isonlydigits(p2, 1)) {
218 strcpy(dayofmonth, p2);
219 *idayofmonth = (int)strtol(p2, (char **)NULL, 10);
220 *flags |= F_DAYOFMONTH;
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 strcpy(dayofweek, getdayofweekname(offset));
233 if (strlen(p2) == len)
235 strcpy(modifierindex, p2 + len);
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 sprintf(dayofmonth, "%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 strcpy(month, getmonthname(d));
269 strcpy(dayofweek, getdayofweekname(offset));
270 if (strlen(p2) == len)
272 strcpy(modifierindex, p2 + len);
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 strcpy(month, getmonthname(d));
295 sprintf(dayofmonth, "%d", m);
299 strcpy(month, getmonthname(m));
300 sprintf(dayofmonth, "%d", d);
316 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
317 int dd, char *extra);
319 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm,
322 static int warned = 0;
324 if (*rememberindex >= MAXCOUNT - 1) {
326 warnx("Index > %d, ignored", MAXCOUNT);
330 y[*rememberindex] = yy;
331 m[*rememberindex] = mm;
332 d[*rememberindex] = dd;
334 strcpy(ed[*rememberindex], extra);
336 ed[*rememberindex][0] = '\0';
341 debug_determinestyle(int dateonly, char *date, int flags, char *month,
342 int imonth, char *dayofmonth, int idayofmonth, char *dayofweek,
343 int idayofweek, char *modifieroffset, char *modifierindex, char *specialday,
344 char *year, int iyear)
348 printf("-------\ndate: |%s|\n", date);
352 printf("flags: %x - %s\n", flags, showflags(flags));
353 if (modifieroffset[0] != '\0')
354 printf("modifieroffset: |%s|\n", modifieroffset);
355 if (modifierindex[0] != '\0')
356 printf("modifierindex: |%s|\n", modifierindex);
358 printf("year: |%s| (%d)\n", year, iyear);
359 if (month[0] != '\0')
360 printf("month: |%s| (%d)\n", month, imonth);
361 if (dayofmonth[0] != '\0')
362 printf("dayofmonth: |%s| (%d)\n", dayofmonth, idayofmonth);
363 if (dayofweek[0] != '\0')
364 printf("dayofweek: |%s| (%d)\n", dayofweek, idayofweek);
365 if (specialday[0] != '\0')
366 printf("specialday: |%s|\n", specialday);
369 static struct yearinfo {
371 int ieaster, ipaskha, firstcnyday;
372 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
373 double ffullmooncny[MAXMOONS], fnewmooncny[MAXMOONS];
374 int ichinesemonths[MAXMOONS];
375 double equinoxdays[2], solsticedays[2];
377 struct yearinfo *next;
381 * Calculate dates with offset from weekdays, like Thurs-3, Wed+2, etc.
382 * day is the day of the week,
383 * offset the ordinal number of the weekday in the month.
386 wdayom (int day, int offset, int month, int year)
388 /* Weekday of first day in month */
389 int wday1; /* first day of month */
390 /* Weekday of last day in month */
394 wday1 = first_dayofweek_of_month(year, month);
395 if (wday1 < 0) /* not set */
398 * Date of zeroth or first of our weekday in month, depending on the
399 * relationship with the first of the month. The range is -6:6.
401 d = (day - wday1 + 1) % 7;
403 * Which way are we counting? Offset 0 is invalid, abs (offset) > 5 is
404 * meaningless, but that's OK. Offset 5 may or may not be meaningless,
405 * so there's no point in complaining for complaining's sake.
407 if (offset < 0) { /* back from end of month */
410 while (wdayn <= yearinfo->monthdays[month])
412 d = offset * 7 + wdayn;
413 } else if (offset > 0){
419 warnx ("Invalid offset 0");
424 * Possible date formats include any combination of:
425 * 3-charmonth (January, Jan, Jan)
426 * 3-charweekday (Friday, Monday, mon.)
427 * numeric month or day (1, 2, 04)
429 * Any character may separate them, or they may not be separated. Any line,
430 * following a line that is matched, that starts with "whitespace", is shown
431 * along with the matched line.
434 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags,
437 char month[100], dayofmonth[100], dayofweek[100], modifieroffset[100];
439 char modifierindex[100], specialday[100];
440 int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1;
442 int d, m, dow, rm, rd, offset;
450 * Monthname: Jan .. Dec
452 * Weekday: Mon .. Sun
459 debug_determinestyle(1, date, *flags, month, imonth,
460 dayofmonth, idayofmonth, dayofweek, idayofweek,
461 modifieroffset, modifierindex, specialday, syear, iyear);
462 if (determinestyle(date, flags, month, &imonth, dayofmonth,
463 &idayofmonth, dayofweek, &idayofweek, modifieroffset,
464 modifierindex, specialday, syear, &iyear) == 0) {
471 debug_determinestyle(0, date, *flags, month, imonth,
472 dayofmonth, idayofmonth, dayofweek, idayofweek,
473 modifieroffset, modifierindex, specialday, syear, iyear);
476 for (year = year1; year <= year2; year++) {
479 /* If the year is specified, only do it if it is this year! */
480 if ((lflags & F_YEAR) != 0)
485 /* Get important dates for this year */
487 while (yearinfo != NULL) {
488 if (yearinfo->year == year)
490 yearinfo = yearinfo -> next;
492 if (yearinfo == NULL) {
493 yearinfo = (struct yearinfo *)calloc(1,
494 sizeof(struct yearinfo));
495 if (yearinfo == NULL)
496 errx(1, "Unable to allocate more years");
497 yearinfo->year = year;
498 yearinfo->next = years;
501 yearinfo->monthdays = monthdaytab[isleap(year)];
502 yearinfo->ieaster = easter(year);
503 yearinfo->ipaskha = paskha(year);
504 fpom(year, UTCOffset, yearinfo->ffullmoon,
506 fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny,
507 yearinfo->fnewmooncny);
508 fequinoxsolstice(year, UTCOffset,
509 yearinfo->equinoxdays, yearinfo->solsticedays);
512 * CNY: Match day with sun longitude at 330` with new
515 yearinfo->firstcnyday = calculatesunlongitude30(year,
516 UTCOFFSET_CNY, yearinfo->ichinesemonths);
517 for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) {
518 if (yearinfo->fnewmooncny[m] >
519 yearinfo->firstcnyday) {
520 yearinfo->firstcnyday =
521 floor(yearinfo->fnewmooncny[m - 1]);
527 /* Same day every year */
528 if (lflags == (F_MONTH | F_DAYOFMONTH)) {
529 if (!remember_ymd(year, imonth, idayofmonth))
531 remember(&remindex, yearp, monthp, dayp, edp,
532 year, imonth, idayofmonth, NULL);
536 /* XXX Same day every year, but variable */
537 if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) {
538 if (!remember_ymd(year, imonth, idayofmonth))
540 remember(&remindex, yearp, monthp, dayp, edp,
541 year, imonth, idayofmonth, NULL);
545 /* Same day every month */
546 if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) {
547 for (m = 1; m <= 12; m++) {
548 if (!remember_ymd(year, m, idayofmonth))
550 remember(&remindex, yearp, monthp, dayp, edp,
551 year, m, idayofmonth, NULL);
556 /* Every day of a month */
557 if (lflags == (F_ALLDAY | F_MONTH)) {
558 for (d = 1; d <= yearinfo->monthdays[imonth]; d++) {
559 if (!remember_ymd(year, imonth, d))
561 remember(&remindex, yearp, monthp, dayp, edp,
562 year, imonth, d, NULL);
567 /* One day of every month */
568 if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) {
569 for (m = 1; m <= 12; m++) {
570 if (!remember_ymd(year, m, idayofmonth))
572 remember(&remindex, yearp, monthp, dayp, edp,
573 year, m, idayofmonth, NULL);
578 /* Every dayofweek of the year */
579 if (lflags == (F_DAYOFWEEK | F_VARIABLE)) {
580 dow = first_dayofweek_of_year(year);
581 d = (idayofweek - dow + 8) % 7;
583 if (remember_yd(year, d, &rm, &rd))
585 yearp, monthp, dayp, edp,
593 * Every so-manied dayofweek of every month of the year:
596 if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
597 offset = indextooffset(modifierindex);
599 for (m = 0; m <= 12; m++) {
600 d = wdayom (idayofweek, offset, m, year);
601 if (remember_ymd(year, m, d)) {
603 yearp, monthp, dayp, edp,
612 * A certain dayofweek of a month
616 (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) {
617 offset = indextooffset(modifierindex);
618 dow = first_dayofweek_of_month(year, imonth);
619 d = (idayofweek - dow + 8) % 7;
622 while (d <= yearinfo->monthdays[imonth]) {
624 && remember_ymd(year, imonth, d)) {
626 yearp, monthp, dayp, edp,
627 year, imonth, d, NULL);
635 while (d <= yearinfo->monthdays[imonth])
637 while (offset != 0) {
641 if (remember_ymd(year, imonth, d))
643 yearp, monthp, dayp, edp,
644 year, imonth, d, NULL);
650 /* Every dayofweek of the month */
651 if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) {
652 dow = first_dayofweek_of_month(year, imonth);
653 d = (idayofweek - dow + 8) % 7;
654 while (d <= yearinfo->monthdays[imonth]) {
655 if (remember_ymd(year, imonth, d))
657 yearp, monthp, dayp, edp,
658 year, imonth, d, NULL);
665 if ((lflags & ~F_MODIFIEROFFSET) ==
666 (F_SPECIALDAY | F_VARIABLE | F_EASTER)) {
668 if ((lflags & F_MODIFIEROFFSET) != 0)
669 offset = parseoffset(modifieroffset);
670 if (remember_yd(year, yearinfo->ieaster + offset,
672 remember(&remindex, yearp, monthp, dayp, edp,
678 if ((lflags & ~F_MODIFIEROFFSET) ==
679 (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) {
681 if ((lflags & F_MODIFIEROFFSET) != 0)
682 offset = parseoffset(modifieroffset);
683 if (remember_yd(year, yearinfo->ipaskha + offset,
685 remember(&remindex, yearp, monthp, dayp, edp,
690 /* Chinese New Year */
691 if ((lflags & ~F_MODIFIEROFFSET) ==
692 (F_SPECIALDAY | F_VARIABLE | F_CNY)) {
694 if ((lflags & F_MODIFIEROFFSET) != 0)
695 offset = parseoffset(modifieroffset);
696 if (remember_yd(year, yearinfo->firstcnyday + offset,
698 remember(&remindex, yearp, monthp, dayp, edp,
704 if ((lflags & ~F_MODIFIEROFFSET) ==
705 (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) {
709 if ((lflags & F_MODIFIEROFFSET) != 0)
710 offset = parseoffset(modifieroffset);
711 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
712 if (remember_yd(year,
713 floor(yearinfo->ffullmoon[i]) + offset,
716 yearinfo->ffullmoon[i]);
718 yearp, monthp, dayp, edp,
726 if ((lflags & ~F_MODIFIEROFFSET) ==
727 (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) {
731 if ((lflags & F_MODIFIEROFFSET) != 0)
732 offset = parseoffset(modifieroffset);
733 for (i = 0; yearinfo->ffullmoon[i] > 0; i++) {
734 if (remember_yd(year,
735 floor(yearinfo->fnewmoon[i]) + offset,
737 ed = floattotime(yearinfo->fnewmoon[i]);
739 yearp, monthp, dayp, edp,
746 /* (Mar|Sep)Equinox */
747 if ((lflags & ~F_MODIFIEROFFSET) ==
748 (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) {
750 if ((lflags & F_MODIFIEROFFSET) != 0)
751 offset = parseoffset(modifieroffset);
752 if (remember_yd(year, yearinfo->equinoxdays[0] + offset,
754 ed = floattotime(yearinfo->equinoxdays[0]);
755 remember(&remindex, yearp, monthp, dayp, edp,
760 if ((lflags & ~F_MODIFIEROFFSET) ==
761 (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) {
763 if ((lflags & F_MODIFIEROFFSET) != 0)
764 offset = parseoffset(modifieroffset);
765 if (remember_yd(year, yearinfo->equinoxdays[1] + offset,
767 ed = floattotime(yearinfo->equinoxdays[1]);
768 remember(&remindex, yearp, monthp, dayp, edp,
774 /* (Jun|Dec)Solstice */
775 if ((lflags & ~F_MODIFIEROFFSET) ==
776 (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) {
778 if ((lflags & F_MODIFIEROFFSET) != 0)
779 offset = parseoffset(modifieroffset);
780 if (remember_yd(year,
781 yearinfo->solsticedays[0] + offset, &rm, &rd)) {
782 ed = floattotime(yearinfo->solsticedays[0]);
783 remember(&remindex, yearp, monthp, dayp, edp,
788 if ((lflags & ~F_MODIFIEROFFSET) ==
789 (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) {
791 if ((lflags & F_MODIFIEROFFSET) != 0)
792 offset = parseoffset(modifieroffset);
793 if (remember_yd(year,
794 yearinfo->solsticedays[1] + offset, &rm, &rd)) {
795 ed = floattotime(yearinfo->solsticedays[1]);
796 remember(&remindex, yearp, monthp, dayp, edp,
803 printf("Unprocessed:\n");
804 debug_determinestyle(2, date, lflags, month, imonth,
805 dayofmonth, idayofmonth, dayofweek, idayofweek,
806 modifieroffset, modifierindex, specialday, syear,
812 if (retvalsign == -1)
813 return (-remindex - 1);
824 if ((flags & F_YEAR) != 0)
826 if ((flags & F_MONTH) != 0)
828 if ((flags & F_DAYOFWEEK) != 0)
829 strcat(s, "dayofweek ");
830 if ((flags & F_DAYOFMONTH) != 0)
831 strcat(s, "dayofmonth ");
832 if ((flags & F_MODIFIERINDEX) != 0)
833 strcat(s, "modifierindex ");
834 if ((flags & F_MODIFIEROFFSET) != 0)
835 strcat(s, "modifieroffset ");
836 if ((flags & F_SPECIALDAY) != 0)
837 strcat(s, "specialday ");
838 if ((flags & F_ALLMONTH) != 0)
839 strcat(s, "allmonth ");
840 if ((flags & F_ALLDAY) != 0)
841 strcat(s, "allday ");
842 if ((flags & F_VARIABLE) != 0)
843 strcat(s, "variable ");
844 if ((flags & F_CNY) != 0)
845 strcat(s, "chinesenewyear ");
846 if ((flags & F_PASKHA) != 0)
847 strcat(s, "paskha ");
848 if ((flags & F_EASTER) != 0)
849 strcat(s, "easter ");
850 if ((flags & F_FULLMOON) != 0)
851 strcat(s, "fullmoon ");
852 if ((flags & F_NEWMOON) != 0)
853 strcat(s, "newmoon ");
854 if ((flags & F_MAREQUINOX) != 0)
855 strcat(s, "marequinox ");
856 if ((flags & F_SEPEQUINOX) != 0)
857 strcat(s, "sepequinox ");
858 if ((flags & F_JUNSOLSTICE) != 0)
859 strcat(s, "junsolstice ");
860 if ((flags & F_DECSOLSTICE) != 0)
861 strcat(s, "decsolstice ");
869 if (i <= 0 || i > 12)
871 if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL)
872 return (nmonths[i - 1].name);
873 return (months[i - 1]);
877 checkmonth(char *s, size_t *len, size_t *offset, const char **month)
882 for (i = 0; fnmonths[i].name != NULL; i++) {
884 if (strncasecmp(s, n->name, n->len) == 0) {
891 for (i = 0; nmonths[i].name != NULL; i++) {
893 if (strncasecmp(s, n->name, n->len) == 0) {
900 for (i = 0; fmonths[i] != NULL; i++) {
901 *len = strlen(fmonths[i]);
902 if (strncasecmp(s, fmonths[i], *len) == 0) {
908 for (i = 0; months[i] != NULL; i++) {
909 if (strncasecmp(s, months[i], 3) == 0) {
920 getdayofweekname(int i)
922 if (ndays[i].len != 0 && ndays[i].name != NULL)
923 return (ndays[i].name);
928 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow)
933 for (i = 0; fndays[i].name != NULL; i++) {
935 if (strncasecmp(s, n->name, n->len) == 0) {
942 for (i = 0; ndays[i].name != NULL; i++) {
944 if (strncasecmp(s, n->name, n->len) == 0) {
951 for (i = 0; fdays[i] != NULL; i++) {
952 *len = strlen(fdays[i]);
953 if (strncasecmp(s, fdays[i], *len) == 0) {
959 for (i = 0; days[i] != NULL; i++) {
960 if (strncasecmp(s, days[i], 3) == 0) {
971 isonlydigits(char *s, int nostar)
974 for (i = 0; s[i] != '\0'; i++) {
975 if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0')
977 if (!isdigit((unsigned char)s[i]))
984 indextooffset(char *s)
990 if (s[0] == '+' || s[0] == '-') {
991 i = strtol (s, &es, 10);
992 if (*es != '\0') /* trailing junk */
993 errx (1, "Invalid specifier format: %s\n", s);
997 for (i = 0; i < 6; i++) {
998 if (strcasecmp(s, sequences[i]) == 0) {
1004 for (i = 0; i < 6; i++) {
1008 if (strncasecmp(s, n->name, n->len) == 0) {
1018 parseoffset(char *s)
1020 return strtol(s, NULL, 10);
1024 floattotime(double f)
1026 static char buf[100];
1032 hh = i / SECSPERHOUR;
1034 mm = i / SECSPERMINUTE;
1038 sprintf(buf, "%02d:%02d:%02d", hh, mm, ss);
1043 floattoday(int year, double f)
1045 static char buf[100];
1046 int i, m, d, hh, mm, ss;
1047 int *cumdays = cumdaytab[isleap(year)];
1049 for (i = 0; 1 + cumdays[i] < f; i++)
1052 d = floor(f - 1 - cumdays[i]);
1056 hh = i / SECSPERHOUR;
1058 mm = i / SECSPERMINUTE;
1062 sprintf(buf, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss);
1071 printf("UTCOffset: %g\n", UTCOffset);
1072 printf("eastlongitude: %d\n", EastLongitude);
1074 if (strcmp(what, "moon") == 0) {
1075 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS];
1078 for (year = year1; year <= year2; year++) {
1079 fpom(year, UTCOffset, ffullmoon, fnewmoon);
1080 printf("Full moon %d:\t", year);
1081 for (i = 0; ffullmoon[i] >= 0; i++) {
1082 printf("%g (%s) ", ffullmoon[i],
1083 floattoday(year, ffullmoon[i]));
1085 printf("\nNew moon %d:\t", year);
1086 for (i = 0; fnewmoon[i] >= 0; i++) {
1087 printf("%g (%s) ", fnewmoon[i],
1088 floattoday(year, fnewmoon[i]));
1097 if (strcmp(what, "sun") == 0) {
1098 double equinoxdays[2], solsticedays[2];
1099 for (year = year1; year <= year2; year++) {
1100 printf("Sun in %d:\n", year);
1101 fequinoxsolstice(year, UTCOffset, equinoxdays,
1103 printf("e[0] - %g (%s)\n",
1105 floattoday(year, equinoxdays[0]));
1106 printf("e[1] - %g (%s)\n",
1108 floattoday(year, equinoxdays[1]));
1109 printf("s[0] - %g (%s)\n",
1111 floattoday(year, solsticedays[0]));
1112 printf("s[1] - %g (%s)\n",
1114 floattoday(year, solsticedays[1]));