2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * $FreeBSD: src/usr.bin/calendar/day.c,v 1.13.2.5 2003/04/06 20:04:57 dwmalone Exp $
34 * $DragonFly: src/usr.bin/calendar/day.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
37 #include <sys/types.h>
47 #include "pathnames.h"
51 int *cumdays, offset, yrdays;
55 /* 1-based month, 0-based days, cumulative */
57 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
58 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
61 static char const *days[] = {
62 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
65 static const char *months[] = {
66 "jan", "feb", "mar", "apr", "may", "jun",
67 "jul", "aug", "sep", "oct", "nov", "dec", NULL,
70 static struct fixs fndays[8]; /* full national days names */
71 static struct fixs ndays[8]; /* short national days names */
73 static struct fixs fnmonths[13]; /* full national months names */
74 static struct fixs nmonths[13]; /* short national month names */
83 for (i = 0; i < 7; i++) {
85 strftime(buf, sizeof(buf), "%a", &tm);
87 l > 0 && isspace((unsigned char)buf[l - 1]);
91 if (ndays[i].name != NULL)
93 if ((ndays[i].name = strdup(buf)) == NULL)
94 errx(1, "cannot allocate memory");
95 ndays[i].len = strlen(buf);
97 strftime(buf, sizeof(buf), "%A", &tm);
99 l > 0 && isspace((unsigned char)buf[l - 1]);
103 if (fndays[i].name != NULL)
104 free(fndays[i].name);
105 if ((fndays[i].name = strdup(buf)) == NULL)
106 errx(1, "cannot allocate memory");
107 fndays[i].len = strlen(buf);
110 for (i = 0; i < 12; i++) {
112 strftime(buf, sizeof(buf), "%b", &tm);
113 for (l = strlen(buf);
114 l > 0 && isspace((unsigned char)buf[l - 1]);
118 if (nmonths[i].name != NULL)
119 free(nmonths[i].name);
120 if ((nmonths[i].name = strdup(buf)) == NULL)
121 errx(1, "cannot allocate memory");
122 nmonths[i].len = strlen(buf);
124 strftime(buf, sizeof(buf), "%B", &tm);
125 for (l = strlen(buf);
126 l > 0 && isspace((unsigned char)buf[l - 1]);
130 if (fnmonths[i].name != NULL)
131 free(fnmonths[i].name);
132 if ((fnmonths[i].name = strdup(buf)) == NULL)
133 errx(1, "cannot allocate memory");
134 fnmonths[i].len = strlen(buf);
144 tp = localtime(&now);
145 if ( isleap(tp->tm_year + 1900) ) {
152 /* Friday displays Monday's events */
153 offset = tp->tm_wday == Friday ? 3 : 1;
154 header[5].iov_base = dayname;
157 lbufp = setlocale(LC_TIME, NULL);
158 if (lbufp != NULL && (oldl = strdup(lbufp)) == NULL)
159 errx(1, "cannot allocate memory");
160 (void) setlocale(LC_TIME, "C");
161 header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
162 (void) setlocale(LC_TIME, (oldl != NULL ? oldl : ""));
169 /* convert Day[/Month][/Year] into unix time (since 1970)
170 * Day: two digits, Month: two digits, Year: digits
186 tm.tm_mday = tp->tm_mday;
187 tm.tm_mon = tp->tm_mon;
188 tm.tm_year = tp->tm_year;
190 switch (sscanf(dp, "%d.%d.%d", &d, &m, &y)) {
204 fprintf(stderr, "Mktime: %d %d %s\n", (int)mktime(&tm), (int)t,
211 * Possible date formats include any combination of:
212 * 3-charmonth (January, Jan, Jan)
213 * 3-charweekday (Friday, Monday, mon.)
214 * numeric month or day (1, 2, 04)
216 * Any character may separate them, or they may not be separated. Any line,
217 * following a line that is matched, that starts with "whitespace", is shown
218 * along with the matched line.
221 isnow(endp, monthp, dayp, varp)
227 int day, flags, month = 0, v1, v2;
233 * Monthname: Jan .. Dec
241 /* read first field */
242 /* didn't recognize anything, skip it */
243 if (!(v1 = getfield(endp, &endp, &flags)))
246 /* Easter or Easter depending days */
247 if (flags & F_EASTER)
248 day = v1 - 1; /* days since January 1 [0-365] */
251 * 1. {Weekday,Day} XYZ ...
255 else if (flags & F_ISDAY || v1 > 12) {
257 /* found a day; day: 1-31 or weekday: 1-7 */
260 /* {Day,Weekday} {Month,Monthname} ... */
261 /* if no recognizable month, assume just a day alone
262 * in other words, find month or use current month */
263 if (!(month = getfield(endp, &endp, &flags)))
264 month = tp->tm_mon + 1;
267 /* 2. {Monthname} XYZ ... */
268 else if (flags & F_ISMONTH) {
271 /* Monthname {day,weekday} */
272 /* if no recognizable day, assume the first day in month */
273 if (!(day = getfield(endp, &endp, &flags)))
279 v2 = getfield(endp, &endp, &flags);
282 * {Day} {Monthname} ...
285 if (flags & F_ISMONTH) {
291 /* {Month} {Weekday,Day} ... */
293 /* F_ISDAY set, v2 > 12, or no way to tell */
295 /* if no recognizable day, assume the first */
301 /* convert Weekday into *next* Day,
302 * e.g.: 'Sunday' -> 22
305 if (flags & F_ISDAY) {
307 fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
311 /* variable weekday, SundayLast, MondayFirst ... */
312 if (day < 0 || day >= 10) {
314 /* negative offset; last, -4 .. -1 */
316 v1 = day/10 - 1; /* offset -4 ... -1 */
317 day = 10 + (day % 10); /* day 1 ... 7 */
320 v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
322 /* (month length - day) / 7 + 1 */
323 if (cumdays[month+1] - cumdays[month] >= v2
324 && ((int)((cumdays[month+1] -
325 cumdays[month] - v2) / 7) + 1) == -v1)
329 /* set to yesterday */
331 day = tp->tm_mday - 1;
337 /* first, second ... +1 ... +5 */
339 v1 = day/10; /* offset: +1 (first Sunday) ... */
343 v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
345 /* Hurrah! matched */
346 if ( ((v2 - 1 + 7) / 7) == v1 )
349 /* set to yesterday */
351 day = tp->tm_mday - 1;
360 day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
365 if (!(flags & F_EASTER)) {
368 day = cumdays[month] + day;
371 for (v1 = 0; day > cumdays[v1]; v1++)
374 *dayp = day - cumdays[v1 - 1];
379 fprintf(stderr, "day2: day %d(%d-%d) yday %d\n", *dayp, day, cumdays[month], tp->tm_yday);
381 /* if today or today + offset days */
382 if (day >= tp->tm_yday - f_dayBefore &&
383 day <= tp->tm_yday + offset + f_dayAfter)
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
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);
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);
435 /* return offset for variable weekdays
436 * -1 -> last weekday in month
437 * +1 -> first weekday in month
450 /* Sun+1 or Wednesday-2
453 /* fprintf(stderr, "x: %s %s %d\n", s, s + offs - 2, offs); */
454 switch(*(s + offs - 2)) {
456 return(-(atoi(s + offs - 1)));
459 return(atoi(s + offs - 1));
465 * some aliases: last, first, second, third, fourth
469 if (offs > 4 && !strcasecmp(s + offs - 4, "last"))
471 else if (offs > 5 && !strcasecmp(s + offs - 5, "first"))
473 else if (offs > 6 && !strcasecmp(s + offs - 6, "second"))
475 else if (offs > 5 && !strcasecmp(s + offs - 5, "third"))
477 else if (offs > 6 && !strcasecmp(s + offs - 6, "fourth"))
481 /* no offset detected */