1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: head/usr.bin/calendar/parsedata.c 326276 2017-11-27 15:37:16Z pfg $ 29 */ 30 31 #include <ctype.h> 32 #include <math.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <err.h> 37 38 #include "calendar.h" 39 40 static char *showflags(int flags); 41 static bool 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); 51 static int determinestyle(char *date, int *flags, char *month, int *imonth, 52 char *dayofmonth, int *idayofmonth, char *dayofweek, int *idayofweek, 53 char *modifieroffset, char *modifierindex, char *specialday, 54 char *year, int *iyear); 55 static void remember(int *rememberindex, int *y, int *m, int *d, char **ed, 56 int yy, int mm, int dd, char *extra); 57 58 /* 59 * Expected styles: 60 * 61 * Date ::= Month . ' ' . DayOfMonth | 62 * Month . ' ' . DayOfWeek . ModifierIndex | 63 * Month . '/' . DayOfMonth | 64 * Month . '/' . DayOfWeek . ModifierIndex | 65 * DayOfMonth . ' ' . Month | 66 * DayOfMonth . '/' . Month | 67 * DayOfWeek . ModifierIndex . ' ' .Month | 68 * DayOfWeek . ModifierIndex . '/' .Month | 69 * DayOfWeek . ModifierIndex | 70 * SpecialDay . ModifierOffset 71 * 72 * Month ::= MonthName | MonthNumber | '*' 73 * MonthNumber ::= '0' ... '9' | '00' ... '09' | '10' ... '12' 74 * MonthName ::= MonthNameShort | MonthNameLong 75 * MonthNameLong ::= 'January' ... 'December' 76 * MonthNameShort ::= 'Jan' ... 'Dec' | 'Jan.' ... 'Dec.' 77 * 78 * DayOfWeek ::= DayOfWeekShort | DayOfWeekLong 79 * DayOfWeekShort ::= 'Mon' .. 'Sun' 80 * DayOfWeekLong ::= 'Monday' .. 'Sunday' 81 * DayOfMonth ::= '0' ... '9' | '00' ... '09' | '10' ... '29' | 82 * '30' ... '31' | '*' 83 * 84 * ModifierOffset ::= '' | '+' . ModifierNumber | '-' . ModifierNumber 85 * ModifierNumber ::= '0' ... '9' | '00' ... '99' | '000' ... '299' | 86 * '300' ... '359' | '360' ... '365' 87 * ModifierIndex ::= 'Second' | 'Third' | 'Fourth' | 'Fifth' | 88 * 'First' | 'Last' 89 * 90 * SpecialDay ::= 'Easter' | 'Paskha' | 'ChineseNewYear' 91 * 92 */ 93 static int 94 determinestyle(char *date, int *flags, 95 char *month, int *imonth, char *dayofmonth, int *idayofmonth, 96 char *dayofweek, int *idayofweek, char *modifieroffset, 97 char *modifierindex, char *specialday, char *year, int *iyear) 98 { 99 char *p, *p1, *p2, *py; 100 const char *dow, *pmonth; 101 char pold; 102 size_t len, offset; 103 104 *flags = F_NONE; 105 *month = '\0'; 106 *imonth = 0; 107 *year = '\0'; 108 *iyear = 0; 109 *dayofmonth = '\0'; 110 *idayofmonth = 0; 111 *dayofweek = '\0'; 112 *idayofweek = 0; 113 *modifieroffset = '\0'; 114 *modifierindex = '\0'; 115 *specialday = '\0'; 116 117 #define CHECKSPECIAL(s1, s2, lens2, type) \ 118 if (s2 != NULL && strncmp(s1, s2, lens2) == 0) { \ 119 *flags |= F_SPECIALDAY; \ 120 *flags |= type; \ 121 *flags |= F_VARIABLE; \ 122 if (strlen(s1) == lens2) { \ 123 strcpy(specialday, s1); \ 124 return (1); \ 125 } \ 126 strncpy(specialday, s1, lens2); \ 127 specialday[lens2] = '\0'; \ 128 strcpy(modifieroffset, s1 + lens2); \ 129 *flags |= F_MODIFIEROFFSET; \ 130 return (1); \ 131 } 132 133 if ((p = strchr(date, ' ')) == NULL) { 134 if ((p = strchr(date, '/')) == NULL) { 135 CHECKSPECIAL(date, STRING_CNY, strlen(STRING_CNY), 136 F_CNY); 137 CHECKSPECIAL(date, ncny.name, ncny.len, F_CNY); 138 CHECKSPECIAL(date, STRING_NEWMOON, 139 strlen(STRING_NEWMOON), F_NEWMOON); 140 CHECKSPECIAL(date, nnewmoon.name, nnewmoon.len, 141 F_NEWMOON); 142 CHECKSPECIAL(date, STRING_FULLMOON, 143 strlen(STRING_FULLMOON), F_FULLMOON); 144 CHECKSPECIAL(date, nfullmoon.name, nfullmoon.len, 145 F_FULLMOON); 146 CHECKSPECIAL(date, STRING_PASKHA, 147 strlen(STRING_PASKHA), F_PASKHA); 148 CHECKSPECIAL(date, npaskha.name, npaskha.len, F_PASKHA); 149 CHECKSPECIAL(date, STRING_EASTER, 150 strlen(STRING_EASTER), F_EASTER); 151 CHECKSPECIAL(date, neaster.name, neaster.len, F_EASTER); 152 CHECKSPECIAL(date, STRING_MAREQUINOX, 153 strlen(STRING_MAREQUINOX), F_MAREQUINOX); 154 CHECKSPECIAL(date, nmarequinox.name, nmarequinox.len, 155 F_SEPEQUINOX); 156 CHECKSPECIAL(date, STRING_SEPEQUINOX, 157 strlen(STRING_SEPEQUINOX), F_SEPEQUINOX); 158 CHECKSPECIAL(date, nsepequinox.name, nsepequinox.len, 159 F_SEPEQUINOX); 160 CHECKSPECIAL(date, STRING_JUNSOLSTICE, 161 strlen(STRING_JUNSOLSTICE), F_JUNSOLSTICE); 162 CHECKSPECIAL(date, njunsolstice.name, njunsolstice.len, 163 F_JUNSOLSTICE); 164 CHECKSPECIAL(date, STRING_DECSOLSTICE, 165 strlen(STRING_DECSOLSTICE), F_DECSOLSTICE); 166 CHECKSPECIAL(date, ndecsolstice.name, ndecsolstice.len, 167 F_DECSOLSTICE); 168 if (checkdayofweek(date, &len, &offset, &dow) != 0) { 169 *flags |= F_DAYOFWEEK; 170 *flags |= F_VARIABLE; 171 *idayofweek = offset; 172 if (strlen(date) == len) { 173 strcpy(dayofweek, date); 174 return (1); 175 } 176 strncpy(dayofweek, date, len); 177 dayofweek[len] = '\0'; 178 strcpy(modifierindex, date + len); 179 *flags |= F_MODIFIERINDEX; 180 return (1); 181 } 182 if (isonlydigits(date, 1)) { 183 /* Assume month number only */ 184 *flags |= F_MONTH; 185 *imonth = (int)strtol(date, (char **)NULL, 10); 186 strcpy(month, getmonthname(*imonth)); 187 return(1); 188 } 189 return (0); 190 } 191 } 192 193 /* 194 * After this, leave by goto-ing to "allfine" or "fail" to restore the 195 * original data in `date'. 196 */ 197 pold = *p; 198 *p = 0; 199 p1 = date; 200 p2 = p + 1; 201 /* Now p2 points to the next field and p1 to the first field */ 202 203 if ((py = strchr(p2, '/')) != NULL) { 204 /* We have a year in the string. Now this is getting tricky */ 205 strcpy(year, p1); 206 *iyear = (int)strtol(year, NULL, 10); 207 p1 = p2; 208 p2 = py + 1; 209 *py = 0; 210 *flags |= F_YEAR; 211 } 212 213 /* Check if there is a month-string in the date */ 214 if ((checkmonth(p1, &len, &offset, &pmonth) != 0) 215 || (checkmonth(p2, &len, &offset, &pmonth) != 0 && (p2 = p1))) { 216 /* p2 is the non-month part */ 217 *flags |= F_MONTH; 218 *imonth = offset; 219 220 strcpy(month, getmonthname(offset)); 221 if (isonlydigits(p2, 1)) { 222 strcpy(dayofmonth, p2); 223 *idayofmonth = (int)strtol(p2, (char **)NULL, 10); 224 *flags |= F_DAYOFMONTH; 225 goto allfine; 226 } 227 if (strcmp(p2, "*") == 0) { 228 *flags |= F_ALLDAY; 229 goto allfine; 230 } 231 232 if (checkdayofweek(p2, &len, &offset, &dow) != 0) { 233 *flags |= F_DAYOFWEEK; 234 *flags |= F_VARIABLE; 235 *idayofweek = offset; 236 strcpy(dayofweek, getdayofweekname(offset)); 237 if (strlen(p2) == len) 238 goto allfine; 239 strcpy(modifierindex, p2 + len); 240 *flags |= F_MODIFIERINDEX; 241 goto allfine; 242 } 243 goto fail; 244 } 245 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))) { 249 int d; 250 251 *flags |= F_ALLMONTH; 252 *flags |= F_DAYOFMONTH; 253 d = (int)strtol(p2, (char **)NULL, 10); 254 *idayofmonth = d; 255 sprintf(dayofmonth, "%d", d); 256 goto allfine; 257 } 258 259 /* Month as a number, then a weekday */ 260 if (isonlydigits(p1, 1) 261 && checkdayofweek(p2, &len, &offset, &dow) != 0) { 262 int d; 263 264 *flags |= F_MONTH; 265 *flags |= F_DAYOFWEEK; 266 *flags |= F_VARIABLE; 267 268 *idayofweek = offset; 269 d = (int)strtol(p1, (char **)NULL, 10); 270 *imonth = d; 271 strcpy(month, getmonthname(d)); 272 273 strcpy(dayofweek, getdayofweekname(offset)); 274 if (strlen(p2) == len) 275 goto allfine; 276 strcpy(modifierindex, p2 + len); 277 *flags |= F_MODIFIERINDEX; 278 goto allfine; 279 } 280 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 ambiguous? :-( */ 284 int m, d; 285 286 if (strchr(p2, '*') != NULL) 287 *flags |= F_VARIABLE; 288 289 m = (int)strtol(p1, (char **)NULL, 10); 290 d = (int)strtol(p2, (char **)NULL, 10); 291 292 *flags |= F_MONTH; 293 *flags |= F_DAYOFMONTH; 294 295 if (m > 12) { 296 *imonth = d; 297 *idayofmonth = m; 298 strcpy(month, getmonthname(d)); 299 sprintf(dayofmonth, "%d", m); 300 } else { 301 *imonth = m; 302 *idayofmonth = d; 303 strcpy(month, getmonthname(m)); 304 sprintf(dayofmonth, "%d", d); 305 } 306 goto allfine; 307 } 308 309 fail: 310 *p = pold; 311 return (0); 312 allfine: 313 *p = pold; 314 return (1); 315 } 316 317 static void 318 remember(int *rememberindex, int *y, int *m, int *d, char **ed, int yy, int mm, 319 int dd, char *extra) 320 { 321 static int warned = 0; 322 323 if (*rememberindex >= MAXCOUNT - 1) { 324 if (warned == 0) 325 warnx("Index > %d, ignored", MAXCOUNT); 326 warned++; 327 return; 328 } 329 y[*rememberindex] = yy; 330 m[*rememberindex] = mm; 331 d[*rememberindex] = dd; 332 if (extra != NULL) 333 strcpy(ed[*rememberindex], extra); 334 else 335 ed[*rememberindex][0] = '\0'; 336 *rememberindex += 1; 337 } 338 339 static void 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) 344 { 345 if (dateonly != 0) { 346 printf("-------\ndate: |%s|\n", date); 347 if (dateonly == 1) 348 return; 349 } 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); 355 if (year[0] != '\0') 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); 365 } 366 367 static struct yearinfo { 368 int year; 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]; 374 int *monthdays; 375 struct yearinfo *next; 376 } *years, *yearinfo; 377 378 /* 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. 382 */ 383 static int 384 wdayom(int day, int offset, int month, int year) 385 { 386 int wday1; /* Weekday of first day in month */ 387 int wdayn; /* Weekday of first day in month */ 388 int d; 389 390 wday1 = first_dayofweek_of_month(year, month); 391 if (wday1 < 0) /* not set */ 392 return (wday1); 393 394 /* 395 * Date of zeroth or first of our weekday in month, depending on the 396 * relationship with the first of the month. The range is -6:6. 397 */ 398 d = (day - wday1 + 1) % 7; 399 /* 400 * Which way are we counting? Offset 0 is invalid, abs (offset) > 5 is 401 * meaningless, but that's OK. Offset 5 may or may not be meaningless, 402 * so there's no point in complaining for complaining's sake. 403 */ 404 if (offset < 0) { /* back from end of month */ 405 /* FIXME */ 406 wdayn = d; 407 while (wdayn <= yearinfo->monthdays[month]) 408 wdayn += 7; 409 d = offset * 7 + wdayn; 410 } else if (offset > 0){ 411 if (d > 0) 412 d += offset * 7 - 7; 413 else 414 d += offset * 7; 415 } else { 416 warnx ("Invalid offset 0"); 417 } 418 return (d); 419 } 420 421 /* 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) 426 * 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. 430 */ 431 int 432 parsedaymonth(char *date, int *yearp, int *monthp, int *dayp, int *flags, 433 char **edp) 434 { 435 char month[100], dayofmonth[100], dayofweek[100], modifieroffset[100]; 436 char syear[100]; 437 char modifierindex[100], specialday[100]; 438 int idayofweek = -1, imonth = -1, idayofmonth = -1, iyear = -1; 439 int year, remindex; 440 int d, m, dow, rm, rd, offset; 441 char *ed; 442 int retvalsign = 1; 443 444 /* 445 * CONVENTION 446 * 447 * Month: 1-12 448 * Monthname: Jan .. Dec 449 * Day: 1-31 450 * Weekday: Mon .. Sun 451 */ 452 453 *flags = 0; 454 455 if (debug) 456 debug_determinestyle(1, date, *flags, month, imonth, 457 dayofmonth, idayofmonth, dayofweek, idayofweek, 458 modifieroffset, modifierindex, specialday, syear, iyear); 459 if (determinestyle(date, flags, month, &imonth, dayofmonth, 460 &idayofmonth, dayofweek, &idayofweek, modifieroffset, 461 modifierindex, specialday, syear, &iyear) == 0) { 462 if (debug) 463 printf("Failed!\n"); 464 return (0); 465 } 466 467 if (debug) 468 debug_determinestyle(0, date, *flags, month, imonth, 469 dayofmonth, idayofmonth, dayofweek, idayofweek, 470 modifieroffset, modifierindex, specialday, syear, iyear); 471 472 remindex = 0; 473 for (year = year1; year <= year2; year++) { 474 475 int lflags = *flags; 476 /* If the year is specified, only do it if it is this year! */ 477 if ((lflags & F_YEAR) != 0) 478 if (iyear != year) 479 continue; 480 lflags &= ~F_YEAR; 481 482 /* Get important dates for this year */ 483 yearinfo = years; 484 while (yearinfo != NULL) { 485 if (yearinfo->year == year) 486 break; 487 yearinfo = yearinfo -> next; 488 } 489 if (yearinfo == NULL) { 490 yearinfo = (struct yearinfo *)calloc(1, 491 sizeof(struct yearinfo)); 492 if (yearinfo == NULL) 493 errx(1, "Unable to allocate more years"); 494 yearinfo->year = year; 495 yearinfo->next = years; 496 years = yearinfo; 497 498 yearinfo->monthdays = monthdaytab[isleap(year)]; 499 yearinfo->ieaster = easter(year); 500 yearinfo->ipaskha = paskha(year); 501 fpom(year, UTCOffset, yearinfo->ffullmoon, 502 yearinfo->fnewmoon); 503 fpom(year, UTCOFFSET_CNY, yearinfo->ffullmooncny, 504 yearinfo->fnewmooncny); 505 fequinoxsolstice(year, UTCOffset, 506 yearinfo->equinoxdays, yearinfo->solsticedays); 507 508 /* 509 * CNY: Match day with sun longitude at 330` with new 510 * moon 511 */ 512 yearinfo->firstcnyday = calculatesunlongitude30(year, 513 UTCOFFSET_CNY, yearinfo->ichinesemonths); 514 for (m = 0; yearinfo->fnewmooncny[m] >= 0; m++) { 515 if (yearinfo->fnewmooncny[m] > 516 yearinfo->firstcnyday) { 517 yearinfo->firstcnyday = 518 floor(yearinfo->fnewmooncny[m - 1]); 519 break; 520 } 521 } 522 } 523 524 /* Same day every year */ 525 if (lflags == (F_MONTH | F_DAYOFMONTH)) { 526 if (!remember_ymd(year, imonth, idayofmonth)) 527 continue; 528 remember(&remindex, yearp, monthp, dayp, edp, 529 year, imonth, idayofmonth, NULL); 530 continue; 531 } 532 533 /* XXX Same day every year, but variable */ 534 if (lflags == (F_MONTH | F_DAYOFMONTH | F_VARIABLE)) { 535 if (!remember_ymd(year, imonth, idayofmonth)) 536 continue; 537 remember(&remindex, yearp, monthp, dayp, edp, 538 year, imonth, idayofmonth, NULL); 539 continue; 540 } 541 542 /* Same day every month */ 543 if (lflags == (F_ALLMONTH | F_DAYOFMONTH)) { 544 for (m = 1; m <= 12; m++) { 545 if (!remember_ymd(year, m, idayofmonth)) 546 continue; 547 remember(&remindex, yearp, monthp, dayp, edp, 548 year, m, idayofmonth, NULL); 549 } 550 continue; 551 } 552 553 /* Every day of a month */ 554 if (lflags == (F_ALLDAY | F_MONTH)) { 555 for (d = 1; d <= yearinfo->monthdays[imonth]; d++) { 556 if (!remember_ymd(year, imonth, d)) 557 continue; 558 remember(&remindex, yearp, monthp, dayp, edp, 559 year, imonth, d, NULL); 560 } 561 continue; 562 } 563 564 /* One day of every month */ 565 if (lflags == (F_ALLMONTH | F_DAYOFWEEK)) { 566 for (m = 1; m <= 12; m++) { 567 if (!remember_ymd(year, m, idayofmonth)) 568 continue; 569 remember(&remindex, yearp, monthp, dayp, edp, 570 year, m, idayofmonth, NULL); 571 } 572 continue; 573 } 574 575 /* Every dayofweek of the year */ 576 if (lflags == (F_DAYOFWEEK | F_VARIABLE)) { 577 dow = first_dayofweek_of_year(year); 578 d = (idayofweek - dow + 8) % 7; 579 while (d <= 366) { 580 if (remember_yd(year, d, &rm, &rd)) 581 remember(&remindex, 582 yearp, monthp, dayp, edp, 583 year, rm, rd, NULL); 584 d += 7; 585 } 586 continue; 587 } 588 589 /* 590 * Every so-manied dayofweek of every month of the year: 591 * Thu-3 592 */ 593 if (lflags == (F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) { 594 offset = indextooffset(modifierindex); 595 596 for (m = 0; m <= 12; m++) { 597 d = wdayom (idayofweek, offset, m, year); 598 if (remember_ymd(year, m, d)) { 599 remember(&remindex, 600 yearp, monthp, dayp, edp, 601 year, m, d, NULL); 602 continue; 603 } 604 } 605 continue; 606 } 607 608 /* 609 * A certain dayofweek of a month 610 * Jan/Thu-3 611 */ 612 if (lflags == 613 (F_MONTH | F_DAYOFWEEK | F_MODIFIERINDEX | F_VARIABLE)) { 614 offset = indextooffset(modifierindex); 615 dow = first_dayofweek_of_month(year, imonth); 616 d = (idayofweek - dow + 8) % 7; 617 618 if (offset > 0) { 619 while (d <= yearinfo->monthdays[imonth]) { 620 if (--offset == 0 621 && remember_ymd(year, imonth, d)) { 622 remember(&remindex, 623 yearp, monthp, dayp, edp, 624 year, imonth, d, NULL); 625 continue; 626 } 627 d += 7; 628 } 629 continue; 630 } 631 if (offset < 0) { 632 while (d <= yearinfo->monthdays[imonth]) 633 d += 7; 634 while (offset != 0) { 635 offset++; 636 d -= 7; 637 } 638 if (remember_ymd(year, imonth, d)) 639 remember(&remindex, 640 yearp, monthp, dayp, edp, 641 year, imonth, d, NULL); 642 continue; 643 } 644 continue; 645 } 646 647 /* Every dayofweek of the month */ 648 if (lflags == (F_DAYOFWEEK | F_MONTH | F_VARIABLE)) { 649 dow = first_dayofweek_of_month(year, imonth); 650 d = (idayofweek - dow + 8) % 7; 651 while (d <= yearinfo->monthdays[imonth]) { 652 if (remember_ymd(year, imonth, d)) 653 remember(&remindex, 654 yearp, monthp, dayp, edp, 655 year, imonth, d, NULL); 656 d += 7; 657 } 658 continue; 659 } 660 661 /* Easter */ 662 if ((lflags & ~F_MODIFIEROFFSET) == 663 (F_SPECIALDAY | F_VARIABLE | F_EASTER)) { 664 offset = 0; 665 if ((lflags & F_MODIFIEROFFSET) != 0) 666 offset = parseoffset(modifieroffset); 667 if (remember_yd(year, yearinfo->ieaster + offset, 668 &rm, &rd)) { 669 remember(&remindex, yearp, monthp, dayp, edp, 670 year, rm, rd, NULL); 671 } 672 continue; 673 } 674 675 /* Paskha */ 676 if ((lflags & ~F_MODIFIEROFFSET) == 677 (F_SPECIALDAY | F_VARIABLE | F_PASKHA)) { 678 offset = 0; 679 if ((lflags & F_MODIFIEROFFSET) != 0) 680 offset = parseoffset(modifieroffset); 681 if (remember_yd(year, yearinfo->ipaskha + offset, 682 &rm, &rd)) { 683 remember(&remindex, yearp, monthp, dayp, edp, 684 year, rm, rd, NULL); 685 } 686 continue; 687 } 688 689 /* Chinese New Year */ 690 if ((lflags & ~F_MODIFIEROFFSET) == 691 (F_SPECIALDAY | F_VARIABLE | F_CNY)) { 692 offset = 0; 693 if ((lflags & F_MODIFIEROFFSET) != 0) 694 offset = parseoffset(modifieroffset); 695 if (remember_yd(year, yearinfo->firstcnyday + offset, 696 &rm, &rd)) { 697 remember(&remindex, yearp, monthp, dayp, edp, 698 year, rm, rd, NULL); 699 } 700 continue; 701 } 702 703 /* FullMoon */ 704 if ((lflags & ~F_MODIFIEROFFSET) == 705 (F_SPECIALDAY | F_VARIABLE | F_FULLMOON)) { 706 int i; 707 708 offset = 0; 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, 714 &rm, &rd)) { 715 ed = floattotime( 716 yearinfo->ffullmoon[i]); 717 remember(&remindex, 718 yearp, monthp, dayp, edp, 719 year, rm, rd, ed); 720 } 721 } 722 continue; 723 } 724 725 /* NewMoon */ 726 if ((lflags & ~F_MODIFIEROFFSET) == 727 (F_SPECIALDAY | F_VARIABLE | F_NEWMOON)) { 728 int i; 729 730 offset = 0; 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, 736 &rm, &rd)) { 737 ed = floattotime(yearinfo->fnewmoon[i]); 738 remember(&remindex, 739 yearp, monthp, dayp, edp, 740 year, rm, rd, ed); 741 } 742 } 743 continue; 744 } 745 746 /* (Mar|Sep)Equinox */ 747 if ((lflags & ~F_MODIFIEROFFSET) == 748 (F_SPECIALDAY | F_VARIABLE | F_MAREQUINOX)) { 749 offset = 0; 750 if ((lflags & F_MODIFIEROFFSET) != 0) 751 offset = parseoffset(modifieroffset); 752 if (remember_yd(year, yearinfo->equinoxdays[0] + offset, 753 &rm, &rd)) { 754 ed = floattotime(yearinfo->equinoxdays[0]); 755 remember(&remindex, yearp, monthp, dayp, edp, 756 year, rm, rd, ed); 757 } 758 continue; 759 } 760 if ((lflags & ~F_MODIFIEROFFSET) == 761 (F_SPECIALDAY | F_VARIABLE | F_SEPEQUINOX)) { 762 offset = 0; 763 if ((lflags & F_MODIFIEROFFSET) != 0) 764 offset = parseoffset(modifieroffset); 765 if (remember_yd(year, yearinfo->equinoxdays[1] + offset, 766 &rm, &rd)) { 767 ed = floattotime(yearinfo->equinoxdays[1]); 768 remember(&remindex, yearp, monthp, dayp, edp, 769 year, rm, rd, ed); 770 } 771 continue; 772 } 773 774 /* (Jun|Dec)Solstice */ 775 if ((lflags & ~F_MODIFIEROFFSET) == 776 (F_SPECIALDAY | F_VARIABLE | F_JUNSOLSTICE)) { 777 offset = 0; 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, 784 year, rm, rd, ed); 785 } 786 continue; 787 } 788 if ((lflags & ~F_MODIFIEROFFSET) == 789 (F_SPECIALDAY | F_VARIABLE | F_DECSOLSTICE)) { 790 offset = 0; 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, 797 year, rm, rd, ed); 798 } 799 continue; 800 } 801 802 if (debug) { 803 printf("Unprocessed:\n"); 804 debug_determinestyle(2, date, lflags, month, imonth, 805 dayofmonth, idayofmonth, dayofweek, idayofweek, 806 modifieroffset, modifierindex, specialday, syear, 807 iyear); 808 } 809 retvalsign = -1; 810 } 811 812 if (retvalsign == -1) 813 return (-remindex - 1); 814 else 815 return (remindex); 816 } 817 818 static char * 819 showflags(int flags) 820 { 821 static char s[1000]; 822 s[0] = '\0'; 823 824 if ((flags & F_YEAR) != 0) 825 strcat(s, "year "); 826 if ((flags & F_MONTH) != 0) 827 strcat(s, "month "); 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 "); 862 863 return s; 864 } 865 866 static const char * 867 getmonthname(int i) 868 { 869 if (i <= 0 || i > 12) 870 return (""); 871 if (nmonths[i - 1].len != 0 && nmonths[i - 1].name != NULL) 872 return (nmonths[i - 1].name); 873 return (months[i - 1]); 874 } 875 876 static int 877 checkmonth(char *s, size_t *len, size_t *offset, const char **month) 878 { 879 struct fixs *n; 880 int i; 881 882 for (i = 0; fnmonths[i].name != NULL; i++) { 883 n = fnmonths + i; 884 if (strncasecmp(s, n->name, n->len) == 0) { 885 *len = n->len; 886 *month = n->name; 887 *offset = i + 1; 888 return (1); 889 } 890 } 891 for (i = 0; nmonths[i].name != NULL; i++) { 892 n = nmonths + i; 893 if (strncasecmp(s, n->name, n->len) == 0) { 894 *len = n->len; 895 *month = n->name; 896 *offset = i + 1; 897 return (1); 898 } 899 } 900 for (i = 0; fmonths[i] != NULL; i++) { 901 *len = strlen(fmonths[i]); 902 if (strncasecmp(s, fmonths[i], *len) == 0) { 903 *month = fmonths[i]; 904 *offset = i + 1; 905 return (1); 906 } 907 } 908 for (i = 0; months[i] != NULL; i++) { 909 if (strncasecmp(s, months[i], 3) == 0) { 910 *len = 3; 911 *month = months[i]; 912 *offset = i + 1; 913 return (1); 914 } 915 } 916 return (0); 917 } 918 919 static const char * 920 getdayofweekname(int i) 921 { 922 if (ndays[i].len != 0 && ndays[i].name != NULL) 923 return (ndays[i].name); 924 return (days[i]); 925 } 926 927 static int 928 checkdayofweek(char *s, size_t *len, size_t *offset, const char **dow) 929 { 930 struct fixs *n; 931 int i; 932 933 for (i = 0; fndays[i].name != NULL; i++) { 934 n = fndays + i; 935 if (strncasecmp(s, n->name, n->len) == 0) { 936 *len = n->len; 937 *dow = n->name; 938 *offset = i; 939 return (1); 940 } 941 } 942 for (i = 0; ndays[i].name != NULL; i++) { 943 n = ndays + i; 944 if (strncasecmp(s, n->name, n->len) == 0) { 945 *len = n->len; 946 *dow = n->name; 947 *offset = i; 948 return (1); 949 } 950 } 951 for (i = 0; fdays[i] != NULL; i++) { 952 *len = strlen(fdays[i]); 953 if (strncasecmp(s, fdays[i], *len) == 0) { 954 *dow = fdays[i]; 955 *offset = i; 956 return (1); 957 } 958 } 959 for (i = 0; days[i] != NULL; i++) { 960 if (strncasecmp(s, days[i], 3) == 0) { 961 *len = 3; 962 *dow = days[i]; 963 *offset = i; 964 return (1); 965 } 966 } 967 return (0); 968 } 969 970 static bool 971 isonlydigits(char *s, int nostar) 972 { 973 int i; 974 for (i = 0; s[i] != '\0'; i++) { 975 if (nostar == 0 && s[i] == '*' && s[i + 1] == '\0') 976 return (true); 977 if (!isdigit((unsigned char)s[i])) 978 return (false); 979 } 980 return (true); 981 } 982 983 static int 984 indextooffset(char *s) 985 { 986 int i; 987 struct fixs *n; 988 char *es; 989 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); 994 return (i); 995 } 996 997 for (i = 0; i < 6; i++) { 998 if (strcasecmp(s, sequences[i]) == 0) { 999 if (i == 5) 1000 return (-1); 1001 return (i + 1); 1002 } 1003 } 1004 for (i = 0; i < 6; i++) { 1005 n = nsequences + i; 1006 if (n->len == 0) 1007 continue; 1008 if (strncasecmp(s, n->name, n->len) == 0) { 1009 if (i == 5) 1010 return (-1); 1011 return (i + 1); 1012 } 1013 } 1014 return (0); 1015 } 1016 1017 static int 1018 parseoffset(char *s) 1019 { 1020 return strtol(s, NULL, 10); 1021 } 1022 1023 static char * 1024 floattotime(double f) 1025 { 1026 static char buf[100]; 1027 int hh, mm, ss, i; 1028 1029 f -= floor(f); 1030 i = f * SECSPERDAY; 1031 1032 hh = i / SECSPERHOUR; 1033 i %= SECSPERHOUR; 1034 mm = i / SECSPERMINUTE; 1035 i %= SECSPERMINUTE; 1036 ss = i; 1037 1038 sprintf(buf, "%02d:%02d:%02d", hh, mm, ss); 1039 return (buf); 1040 } 1041 1042 static char * 1043 floattoday(int year, double f) 1044 { 1045 static char buf[100]; 1046 int i, m, d, hh, mm, ss; 1047 int *cumdays = cumdaytab[isleap(year)]; 1048 1049 for (i = 0; 1 + cumdays[i] < f; i++) 1050 ; 1051 m = --i; 1052 d = floor(f - 1 - cumdays[i]); 1053 f -= floor(f); 1054 i = f * SECSPERDAY; 1055 1056 hh = i / SECSPERHOUR; 1057 i %= SECSPERHOUR; 1058 mm = i / SECSPERMINUTE; 1059 i %= SECSPERMINUTE; 1060 ss = i; 1061 1062 sprintf(buf, "%02d-%02d %02d:%02d:%02d", m, d, hh, mm, ss); 1063 return (buf); 1064 } 1065 1066 void 1067 dodebug(char *what) 1068 { 1069 int year; 1070 1071 printf("UTCOffset: %g\n", UTCOffset); 1072 printf("EastLongitude: %g\n", EastLongitude); 1073 1074 if (strcmp(what, "moon") == 0) { 1075 double ffullmoon[MAXMOONS], fnewmoon[MAXMOONS]; 1076 int i; 1077 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])); 1084 } 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])); 1089 } 1090 printf("\n"); 1091 } 1092 return; 1093 } 1094 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, 1100 solsticedays); 1101 printf("e[0] - %g (%s)\n", 1102 equinoxdays[0], 1103 floattoday(year, equinoxdays[0])); 1104 printf("e[1] - %g (%s)\n", 1105 equinoxdays[1], 1106 floattoday(year, equinoxdays[1])); 1107 printf("s[0] - %g (%s)\n", 1108 solsticedays[0], 1109 floattoday(year, solsticedays[0])); 1110 printf("s[1] - %g (%s)\n", 1111 solsticedays[1], 1112 floattoday(year, solsticedays[1])); 1113 } 1114 return; 1115 } 1116 } 1117