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