1 /* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 35 #include <ctype.h> 36 #include <locale.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <sys/types.h> 40 #include <time.h> 41 #include <sys/uio.h> 42 #include <string.h> 43 #include <stdlib.h> 44 45 #include "pathnames.h" 46 #include "calendar.h" 47 48 struct tm *tp; 49 int *cumdays, offset, yrdays; 50 char dayname[10]; 51 52 53 /* 1-based month, 0-based days, cumulative */ 54 int daytab[][14] = { 55 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 }, 56 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 57 }; 58 59 static char *days[] = { 60 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL, 61 }; 62 63 static char *months[] = { 64 "jan", "feb", "mar", "apr", "may", "jun", 65 "jul", "aug", "sep", "oct", "nov", "dec", NULL, 66 }; 67 68 static struct fixs fndays[8]; /* full national days names */ 69 static struct fixs ndays[8]; /* short national days names */ 70 71 static struct fixs fnmonths[13]; /* full national months names */ 72 static struct fixs nmonths[13]; /* short national month names */ 73 74 75 void setnnames(void) 76 { 77 char buf[80]; 78 int i, l; 79 struct tm tm; 80 81 for (i = 0; i < 7; i++) { 82 tm.tm_wday = i; 83 strftime(buf, sizeof(buf), "%a", &tm); 84 for (l = strlen(buf); 85 l > 0 && isspace((unsigned char)buf[l - 1]); 86 l--) 87 ; 88 buf[l] = '\0'; 89 if (ndays[i].name != NULL) 90 free(ndays[i].name); 91 ndays[i].name = strdup(buf); 92 ndays[i].len = strlen(buf); 93 94 strftime(buf, sizeof(buf), "%A", &tm); 95 for (l = strlen(buf); 96 l > 0 && isspace((unsigned char)buf[l - 1]); 97 l--) 98 ; 99 buf[l] = '\0'; 100 if (fndays[i].name != NULL) 101 free(fndays[i].name); 102 fndays[i].name = strdup(buf); 103 fndays[i].len = strlen(buf); 104 } 105 106 for (i = 0; i < 12; i++) { 107 tm.tm_mon = i; 108 strftime(buf, sizeof(buf), "%b", &tm); 109 for (l = strlen(buf); 110 l > 0 && isspace((unsigned char)buf[l - 1]); 111 l--) 112 ; 113 buf[l] = '\0'; 114 if (nmonths[i].name != NULL) 115 free(nmonths[i].name); 116 nmonths[i].name = strdup(buf); 117 nmonths[i].len = strlen(buf); 118 119 strftime(buf, sizeof(buf), "%B", &tm); 120 for (l = strlen(buf); 121 l > 0 && isspace((unsigned char)buf[l - 1]); 122 l--) 123 ; 124 buf[l] = '\0'; 125 if (fnmonths[i].name != NULL) 126 free(fnmonths[i].name); 127 fnmonths[i].name = strdup(buf); 128 fnmonths[i].len = strlen(buf); 129 } 130 } 131 132 void 133 settime(now) 134 time_t now; 135 { 136 tp = localtime(&now); 137 if ( isleap(tp->tm_year + 1900) ) { 138 yrdays = 366; 139 cumdays = daytab[1]; 140 } else { 141 yrdays = 365; 142 cumdays = daytab[0]; 143 } 144 /* Friday displays Monday's events */ 145 offset = tp->tm_wday == 5 ? 3 : 1; 146 header[5].iov_base = dayname; 147 148 (void) setlocale(LC_TIME, "C"); 149 header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp); 150 (void) setlocale(LC_TIME, ""); 151 152 setnnames(); 153 } 154 155 /* convert Day[/Month][/Year] into unix time (since 1970) 156 * Day: two digits, Month: two digits, Year: digits 157 */ 158 time_t Mktime (dp) 159 char *dp; 160 { 161 char *date; 162 time_t t; 163 int len; 164 struct tm tm; 165 166 date = strdup(dp); 167 if (date == NULL) { 168 fprintf(stderr, "calendar: strdup failed in Mktime\n"); 169 exit(1); 170 } 171 (void)time(&t); 172 tp = localtime(&t); 173 174 len = strlen(date); 175 tm.tm_sec = 0; 176 tm.tm_min = 0; 177 tm.tm_hour = 0; 178 tm.tm_wday = 0; 179 tm.tm_mday = tp->tm_mday; 180 tm.tm_mon = tp->tm_mon; 181 tm.tm_year = tp->tm_year; 182 183 184 /* day */ 185 *(date+2) = NULL; 186 tm.tm_mday = atoi(date); 187 188 /* month */ 189 if (len >= 4) { 190 *(date+5) = NULL; 191 tm.tm_mon = atoi(date+3) - 1; 192 } 193 194 /* Year */ 195 if (len >= 7) { 196 tm.tm_year = atoi(date+6); 197 198 /* tm_year up 1900 ... */ 199 if (tm.tm_year > 1900) 200 tm.tm_year -= 1900; 201 } 202 203 #if DEBUG 204 printf("Mktime: %d %d %d %s\n", (int)mktime(&tm), (int)t, len, 205 asctime(&tm)); 206 #endif 207 free(date); 208 return(mktime(&tm)); 209 } 210 211 /* 212 * Possible date formats include any combination of: 213 * 3-charmonth (January, Jan, Jan) 214 * 3-charweekday (Friday, Monday, mon.) 215 * numeric month or day (1, 2, 04) 216 * 217 * Any character may separate them, or they may not be separated. Any line, 218 * following a line that is matched, that starts with "whitespace", is shown 219 * along with the matched line. 220 */ 221 int 222 isnow(endp, monthp, dayp, varp) 223 char *endp; 224 int *monthp; 225 int *dayp; 226 int *varp; 227 { 228 int day, flags, month = 0, v1, v2; 229 230 /* 231 * CONVENTION 232 * 233 * Month: 1-12 234 * Monthname: Jan .. Dec 235 * Day: 1-31 236 * Weekday: Mon-Sun 237 * 238 */ 239 240 flags = 0; 241 242 /* read first field */ 243 /* didn't recognize anything, skip it */ 244 if (!(v1 = getfield(endp, &endp, &flags))) 245 return (0); 246 247 /* Easter or Easter depending days */ 248 if (flags & F_EASTER) 249 day = v1 - 1; /* days since January 1 [0-365] */ 250 251 /* 252 * 1. {Weekday,Day} XYZ ... 253 * 254 * where Day is > 12 255 */ 256 else if (flags & F_ISDAY || v1 > 12) { 257 258 /* found a day; day: 1-31 or weekday: 1-7 */ 259 day = v1; 260 261 /* {Day,Weekday} {Month,Monthname} ... */ 262 /* if no recognizable month, assume just a day alone 263 * in other words, find month or use current month */ 264 if (!(month = getfield(endp, &endp, &flags))) 265 month = tp->tm_mon + 1; 266 } 267 268 /* 2. {Monthname} XYZ ... */ 269 else if (flags & F_ISMONTH) { 270 month = v1; 271 272 /* Monthname {day,weekday} */ 273 /* if no recognizable day, assume the first day in month */ 274 if (!(day = getfield(endp, &endp, &flags))) 275 day = 1; 276 } 277 278 /* Hm ... */ 279 else { 280 v2 = getfield(endp, &endp, &flags); 281 282 /* 283 * {Day} {Monthname} ... 284 * where Day <= 12 285 */ 286 if (flags & F_ISMONTH) { 287 day = v1; 288 month = v2; 289 *varp = 0; 290 } 291 292 /* {Month} {Weekday,Day} ... */ 293 else { 294 /* F_ISDAY set, v2 > 12, or no way to tell */ 295 month = v1; 296 /* if no recognizable day, assume the first */ 297 day = v2 ? v2 : 1; 298 *varp = 0; 299 } 300 } 301 302 /* convert Weekday into *next* Day, 303 * e.g.: 'Sunday' -> 22 304 * 'SunayLast' -> ?? 305 */ 306 if (flags & F_ISDAY) { 307 #if DEBUG 308 fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month); 309 #endif 310 311 *varp = 1; 312 /* variable weekday, SundayLast, MondayFirst ... */ 313 if (day < 0 || day >= 10) { 314 315 /* negative offset; last, -4 .. -1 */ 316 if (day < 0) { 317 v1 = day/10 - 1; /* offset -4 ... -1 */ 318 day = 10 + (day % 10); /* day 1 ... 7 */ 319 320 /* day, eg '22th' */ 321 v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); 322 323 /* (month length - day) / 7 + 1 */ 324 if (((int)((cumdays[month+1] - 325 cumdays[month] - v2) / 7) + 1) == -v1) 326 /* bingo ! */ 327 day = v2; 328 329 /* set to yesterday */ 330 else { 331 day = tp->tm_mday - 1; 332 if (day == 0) 333 return (0); 334 } 335 } 336 337 /* first, second ... +1 ... +5 */ 338 else { 339 v1 = day/10; /* offset: +1 (first Sunday) ... */ 340 day = day % 10; 341 342 /* day, eg '22th' */ 343 v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); 344 345 /* Hurrah! matched */ 346 if ( ((v2 - 1 + 7) / 7) == v1 ) 347 day = v2; 348 349 /* set to yesterday */ 350 else { 351 day = tp->tm_mday - 1; 352 if (day == 0) 353 return (0); 354 } 355 } 356 } 357 358 /* wired */ 359 else { 360 day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); 361 *varp = 1; 362 } 363 } 364 365 if (!(flags & F_EASTER)) { 366 *monthp = month; 367 *dayp = day; 368 day = cumdays[month] + day; 369 } 370 else { 371 for (v1 = 0; day > cumdays[v1]; v1++) 372 ; 373 *monthp = v1 - 1; 374 *dayp = day - cumdays[v1 - 1]; 375 *varp = 1; 376 } 377 378 #if DEBUG 379 fprintf(stderr, "day2: day %d(%d) yday %d\n", *dayp, day, tp->tm_yday); 380 #endif 381 /* if today or today + offset days */ 382 if (day >= tp->tm_yday - f_dayBefore && 383 day <= tp->tm_yday + offset + f_dayAfter) 384 return (1); 385 386 /* if number of days left in this year + days to event in next year */ 387 if (yrdays - tp->tm_yday + day <= offset + f_dayAfter || 388 /* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */ 389 tp->tm_yday + day - f_dayBefore < 0 390 ) 391 return (1); 392 return (0); 393 } 394 395 396 int 397 getmonth(s) 398 register char *s; 399 { 400 register char **p; 401 struct fixs *n; 402 403 for (n = fnmonths; n->name; ++n) 404 if (!strncasecmp(s, n->name, n->len)) 405 return ((n - fnmonths) + 1); 406 for (n = nmonths; n->name; ++n) 407 if (!strncasecmp(s, n->name, n->len)) 408 return ((n - nmonths) + 1); 409 for (p = months; *p; ++p) 410 if (!strncasecmp(s, *p, 3)) 411 return ((p - months) + 1); 412 return (0); 413 } 414 415 416 int 417 getday(s) 418 register char *s; 419 { 420 register char **p; 421 struct fixs *n; 422 423 for (n = fndays; n->name; ++n) 424 if (!strncasecmp(s, n->name, n->len)) 425 return ((n - fndays) + 1); 426 for (n = ndays; n->name; ++n) 427 if (!strncasecmp(s, n->name, n->len)) 428 return ((n - ndays) + 1); 429 for (p = days; *p; ++p) 430 if (!strncasecmp(s, *p, 3)) 431 return ((p - days) + 1); 432 return (0); 433 } 434 435 /* return offset for variable weekdays 436 * -1 -> last weekday in month 437 * +1 -> first weekday in month 438 * ... etc ... 439 */ 440 int 441 getdayvar(s) 442 register char *s; 443 { 444 register int offset; 445 446 447 offset = strlen(s); 448 449 450 /* Sun+1 or Wednesday-2 451 * ^ ^ */ 452 453 /* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */ 454 switch(*(s + offset - 2)) { 455 case '-': 456 return(-(atoi(s + offset - 1))); 457 break; 458 case '+': 459 return(atoi(s + offset - 1)); 460 break; 461 } 462 463 464 /* 465 * some aliases: last, first, second, third, fourth 466 */ 467 468 /* last */ 469 if (offset > 4 && !strcasecmp(s + offset - 4, "last")) 470 return(-1); 471 else if (offset > 5 && !strcasecmp(s + offset - 5, "first")) 472 return(+1); 473 else if (offset > 6 && !strcasecmp(s + offset - 6, "second")) 474 return(+2); 475 else if (offset > 5 && !strcasecmp(s + offset - 5, "third")) 476 return(+3); 477 else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth")) 478 return(+4); 479 480 481 /* no offset detected */ 482 return(0); 483 } 484