day.c revision 21615
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
48struct tm *tp;
49int *cumdays, offset, yrdays;
50char dayname[10];
51
52
53/* 1-based month, 0-based days, cumulative */
54int 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
59static char *days[] = {
60	"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
61};
62
63static char *months[] = {
64	"jan", "feb", "mar", "apr", "may", "jun",
65	"jul", "aug", "sep", "oct", "nov", "dec", NULL,
66};
67
68static struct fixs fndays[8];         /* full national days names */
69static struct fixs ndays[8];          /* short national days names */
70
71static struct fixs fnmonths[13];      /* full national months names */
72static struct fixs nmonths[13];       /* short national month names */
73
74
75void 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
132void
133settime(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 */
158time_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 */
221int
222isnow(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		}
333
334		/* first, second ... +1 ... +5 */
335		else {
336		    v1 = day/10;        /* offset: +1 (first Sunday) ... */
337		    day = day % 10;
338
339		    /* day, eg '22th' */
340		    v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
341
342		    /* Hurrah! matched */
343		    if ( ((v2 - 1 + 7) / 7) == v1 )
344			day = v2;
345
346		    /* set to yesterday */
347		    else
348			day = tp->tm_mday - 1;
349		}
350	    }
351
352	    /* wired */
353	    else {
354		day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
355		*varp = 1;
356	    }
357	}
358
359	if (!(flags & F_EASTER)) {
360	    *monthp = month;
361	    *dayp = day;
362	    day = cumdays[month] + day;
363	}
364	else {
365	    for (v1 = 0; day > cumdays[v1]; v1++)
366		;
367	    *monthp = v1 - 1;
368	    *dayp = day - cumdays[v1 - 1];
369	    *varp = 1;
370	}
371
372#if DEBUG
373	fprintf(stderr, "day2: day %d(%d) yday %d\n", *dayp, day, tp->tm_yday);
374#endif
375	/* if today or today + offset days */
376	if (day >= tp->tm_yday - f_dayBefore &&
377	    day <= tp->tm_yday + offset + f_dayAfter)
378		return (1);
379
380	/* if number of days left in this year + days to event in next year */
381	if (yrdays - tp->tm_yday + day <= offset + f_dayAfter ||
382	    /* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */
383	    tp->tm_yday + day - f_dayBefore < 0
384	    )
385		return (1);
386	return (0);
387}
388
389
390int
391getmonth(s)
392	register char *s;
393{
394	register char **p;
395	struct fixs *n;
396
397	for (n = fnmonths; n->name; ++n)
398		if (!strncasecmp(s, n->name, n->len))
399			return ((n - fnmonths) + 1);
400	for (n = nmonths; n->name; ++n)
401		if (!strncasecmp(s, n->name, n->len))
402			return ((n - nmonths) + 1);
403	for (p = months; *p; ++p)
404		if (!strncasecmp(s, *p, 3))
405			return ((p - months) + 1);
406	return (0);
407}
408
409
410int
411getday(s)
412	register char *s;
413{
414	register char **p;
415	struct fixs *n;
416
417	for (n = fndays; n->name; ++n)
418		if (!strncasecmp(s, n->name, n->len))
419			return ((n - fndays) + 1);
420	for (n = ndays; n->name; ++n)
421		if (!strncasecmp(s, n->name, n->len))
422			return ((n - ndays) + 1);
423	for (p = days; *p; ++p)
424		if (!strncasecmp(s, *p, 3))
425			return ((p - days) + 1);
426	return (0);
427}
428
429/* return offset for variable weekdays
430 * -1 -> last weekday in month
431 * +1 -> first weekday in month
432 * ... etc ...
433 */
434int
435getdayvar(s)
436	register char *s;
437{
438	register int offset;
439
440
441	offset = strlen(s);
442
443
444	/* Sun+1 or Wednesday-2
445	 *    ^              ^   */
446
447	/* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */
448	switch(*(s + offset - 2)) {
449	case '-':
450	    return(-(atoi(s + offset - 1)));
451	    break;
452	case '+':
453	    return(atoi(s + offset - 1));
454	    break;
455	}
456
457
458	/*
459	 * some aliases: last, first, second, third, fourth
460	 */
461
462	/* last */
463	if      (offset > 4 && !strcasecmp(s + offset - 4, "last"))
464	    return(-1);
465	else if (offset > 5 && !strcasecmp(s + offset - 5, "first"))
466	    return(+1);
467	else if (offset > 6 && !strcasecmp(s + offset - 6, "second"))
468	    return(+2);
469	else if (offset > 5 && !strcasecmp(s + offset - 5, "third"))
470	    return(+3);
471	else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth"))
472	    return(+4);
473
474
475	/* no offset detected */
476	return(0);
477}
478