xref: /freebsd/usr.bin/calendar/day.c (revision 8e537d168674d6b65869f73c20813001af875738)
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 (date)
159     char *date;
160 {
161     time_t t;
162     int len;
163     struct tm tm;
164 
165     (void)time(&t);
166     tp = localtime(&t);
167 
168     len = strlen(date);
169     tm.tm_sec = 0;
170     tm.tm_min = 0;
171     tm.tm_hour = 0;
172     tm.tm_wday = 0;
173     tm.tm_mday = tp->tm_mday;
174     tm.tm_mon = tp->tm_mon;
175     tm.tm_year = tp->tm_year;
176 
177 
178     /* day */
179     *(date+2) = NULL;
180     tm.tm_mday = atoi(date);
181 
182     /* month */
183     if (len >= 4) {
184 	*(date+5) = NULL;
185 	tm.tm_mon = atoi(date+3) - 1;
186     }
187 
188     /* Year */
189     if (len >= 7) {
190 	tm.tm_year = atoi(date+6);
191 
192 	/* tm_year up 1900 ... */
193 	if (tm.tm_year > 1900)
194 	    tm.tm_year -= 1900;
195     }
196 
197 #if DEBUG
198     printf("Mktime: %d %d %d %s\n", (int)mktime(&tm), (int)t, len,
199 	   asctime(&tm));
200 #endif
201     return(mktime(&tm));
202 }
203 
204 /*
205  * Possible date formats include any combination of:
206  *	3-charmonth			(January, Jan, Jan)
207  *	3-charweekday			(Friday, Monday, mon.)
208  *	numeric month or day		(1, 2, 04)
209  *
210  * Any character may separate them, or they may not be separated.  Any line,
211  * following a line that is matched, that starts with "whitespace", is shown
212  * along with the matched line.
213  */
214 int
215 isnow(endp, monthp, dayp, varp)
216 	char *endp;
217 	int	*monthp;
218 	int	*dayp;
219 	int	*varp;
220 {
221 	int day, flags, month = 0, v1, v2;
222 
223 	/*
224 	 * CONVENTION
225 	 *
226 	 * Month:     1-12
227 	 * Monthname: Jan .. Dec
228 	 * Day:       1-31
229 	 * Weekday:   Mon-Sun
230 	 *
231 	 */
232 
233 	flags = 0;
234 
235 	/* read first field */
236 	/* didn't recognize anything, skip it */
237 	if (!(v1 = getfield(endp, &endp, &flags)))
238 		return (0);
239 
240 	/* Easter or Easter depending days */
241 	if (flags & F_EASTER)
242 	    day = v1 - 1; /* days since January 1 [0-365] */
243 
244 	 /*
245 	  * 1. {Weekday,Day} XYZ ...
246 	  *
247 	  *    where Day is > 12
248 	  */
249 	else if (flags & F_ISDAY || v1 > 12) {
250 
251 		/* found a day; day: 1-31 or weekday: 1-7 */
252 		day = v1;
253 
254 		/* {Day,Weekday} {Month,Monthname} ... */
255 		/* if no recognizable month, assume just a day alone
256 		 * in other words, find month or use current month */
257 		if (!(month = getfield(endp, &endp, &flags)))
258 			month = tp->tm_mon + 1;
259 	}
260 
261 	/* 2. {Monthname} XYZ ... */
262 	else if (flags & F_ISMONTH) {
263 		month = v1;
264 
265 		/* Monthname {day,weekday} */
266 		/* if no recognizable day, assume the first day in month */
267 		if (!(day = getfield(endp, &endp, &flags)))
268 			day = 1;
269 	}
270 
271 	/* Hm ... */
272 	else {
273 		v2 = getfield(endp, &endp, &flags);
274 
275 		/*
276 		 * {Day} {Monthname} ...
277 		 * where Day <= 12
278 		 */
279 		if (flags & F_ISMONTH) {
280 			day = v1;
281 			month = v2;
282 			*varp = 0;
283 		}
284 
285 		/* {Month} {Weekday,Day} ...  */
286 		else {
287 			/* F_ISDAY set, v2 > 12, or no way to tell */
288 			month = v1;
289 			/* if no recognizable day, assume the first */
290 			day = v2 ? v2 : 1;
291 			*varp = 0;
292 		}
293 	}
294 
295 	/* convert Weekday into *next*  Day,
296 	 * e.g.: 'Sunday' -> 22
297 	 *       'SunayLast' -> ??
298 	 */
299 	if (flags & F_ISDAY) {
300 #if DEBUG
301 	    fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
302 #endif
303 
304 	    *varp = 1;
305 	    /* variable weekday, SundayLast, MondayFirst ... */
306 	    if (day < 0 || day >= 10) {
307 
308 		/* negative offset; last, -4 .. -1 */
309 		if (day < 0) {
310 		    v1 = day/10 - 1;          /* offset -4 ... -1 */
311 	            day = 10 + (day % 10);    /* day 1 ... 7 */
312 
313 		    /* day, eg '22th' */
314 		    v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
315 
316 		    /* (month length - day) / 7 + 1 */
317 		    if (((int)((cumdays[month+1] -
318 		               cumdays[month] - v2) / 7) + 1) == -v1)
319 			/* bingo ! */
320 			day = v2;
321 
322 		    /* set to yesterday */
323 		    else
324 			day = tp->tm_mday - 1;
325 		}
326 
327 		/* first, second ... +1 ... +5 */
328 		else {
329 		    v1 = day/10;        /* offset: +1 (first Sunday) ... */
330 		    day = day % 10;
331 
332 		    /* day, eg '22th' */
333 		    v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
334 
335 		    /* Hurrah! matched */
336 		    if ( ((v2 - 1 + 7) / 7) == v1 )
337 			day = v2;
338 
339 		    /* set to yesterday */
340 		    else
341 			day = tp->tm_mday - 1;
342 		}
343 	    }
344 
345 	    /* wired */
346 	    else {
347 		day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
348 		*varp = 1;
349 	    }
350 	}
351 
352 	if (!(flags & F_EASTER)) {
353 	    *monthp = month;
354 	    *dayp = day;
355 	    day = cumdays[month] + day;
356 	}
357 	else {
358 	    for (v1 = 0; day > cumdays[v1]; v1++)
359 		;
360 	    *monthp = v1 - 1;
361 	    *dayp = day - cumdays[v1 - 1];
362 	    *varp = 1;
363 	}
364 
365 #if DEBUG
366 	fprintf(stderr, "day2: day %d(%d) yday %d\n", *dayp, day, tp->tm_yday);
367 #endif
368 	/* if today or today + offset days */
369 	if (day >= tp->tm_yday - f_dayBefore &&
370 	    day <= tp->tm_yday + offset + f_dayAfter)
371 		return (1);
372 
373 	/* if number of days left in this year + days to event in next year */
374 	if (yrdays - tp->tm_yday + day <= offset + f_dayAfter ||
375 	    /* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */
376 	    tp->tm_yday + day - f_dayBefore < 0
377 	    )
378 		return (1);
379 	return (0);
380 }
381 
382 
383 int
384 getmonth(s)
385 	register char *s;
386 {
387 	register char **p;
388 	struct fixs *n;
389 
390 	for (n = fnmonths; n->name; ++n)
391 		if (!strncasecmp(s, n->name, n->len))
392 			return ((n - fnmonths) + 1);
393 	for (n = nmonths; n->name; ++n)
394 		if (!strncasecmp(s, n->name, n->len))
395 			return ((n - nmonths) + 1);
396 	for (p = months; *p; ++p)
397 		if (!strncasecmp(s, *p, 3))
398 			return ((p - months) + 1);
399 	return (0);
400 }
401 
402 
403 int
404 getday(s)
405 	register char *s;
406 {
407 	register char **p;
408 	struct fixs *n;
409 
410 	for (n = fndays; n->name; ++n)
411 		if (!strncasecmp(s, n->name, n->len))
412 			return ((n - fndays) + 1);
413 	for (n = ndays; n->name; ++n)
414 		if (!strncasecmp(s, n->name, n->len))
415 			return ((n - ndays) + 1);
416 	for (p = days; *p; ++p)
417 		if (!strncasecmp(s, *p, 3))
418 			return ((p - days) + 1);
419 	return (0);
420 }
421 
422 /* return offset for variable weekdays
423  * -1 -> last weekday in month
424  * +1 -> first weekday in month
425  * ... etc ...
426  */
427 int
428 getdayvar(s)
429 	register char *s;
430 {
431 	register int offset;
432 
433 
434 	offset = strlen(s);
435 
436 
437 	/* Sun+1 or Wednesday-2
438 	 *    ^              ^   */
439 
440 	/* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */
441 	switch(*(s + offset - 2)) {
442 	case '-':
443 	    return(-(atoi(s + offset - 1)));
444 	    break;
445 	case '+':
446 	    return(atoi(s + offset - 1));
447 	    break;
448 	}
449 
450 
451 	/*
452 	 * some aliases: last, first, second, third, fourth
453 	 */
454 
455 	/* last */
456 	if      (offset > 4 && !strcasecmp(s + offset - 4, "last"))
457 	    return(-1);
458 	else if (offset > 5 && !strcasecmp(s + offset - 5, "first"))
459 	    return(+1);
460 	else if (offset > 6 && !strcasecmp(s + offset - 6, "second"))
461 	    return(+2);
462 	else if (offset > 5 && !strcasecmp(s + offset - 5, "third"))
463 	    return(+3);
464 	else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth"))
465 	    return(+4);
466 
467 
468 	/* no offset detected */
469 	return(0);
470 }
471