MFC:
[dragonfly.git] / usr.bin / calendar / day.c
blob41308bd60ef571c3b262024f15459a8ade039bd6
1 /*
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
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.
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.
33 * $FreeBSD: src/usr.bin/calendar/day.c,v 1.27 2007/06/09 05:54:13 grog Exp $
34 * $DragonFly: src/usr.bin/calendar/day.c,v 1.6 2007/09/24 20:31:44 pavalos Exp $
37 #include <sys/types.h>
38 #include <sys/uio.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <locale.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
47 #include "pathnames.h"
48 #include "calendar.h"
50 extern struct iovec header[];
52 struct tm *tp;
53 static const struct tm tm0;
54 int *cumdays, yrdays;
56 static char dayname[10];
58 /* 1-based month, 0-based days, cumulative */
59 static int daytab[][14] = {
60 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
61 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
64 static char const *days[] = {
65 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
68 static const char *months[] = {
69 "jan", "feb", "mar", "apr", "may", "jun",
70 "jul", "aug", "sep", "oct", "nov", "dec", NULL,
73 static struct fixs fndays[8]; /* full national days names */
74 static struct fixs ndays[8]; /* short national days names */
76 static struct fixs fnmonths[13]; /* full national months names */
77 static struct fixs nmonths[13]; /* short national month names */
79 void
80 setnnames(void)
82 char buf[80];
83 int i, l;
84 struct tm tm;
86 for (i = 0; i < 7; i++) {
87 tm.tm_wday = i;
88 strftime(buf, sizeof(buf), "%a", &tm);
89 for (l = strlen(buf);
90 l > 0 && isspace((unsigned char)buf[l - 1]);
91 l--)
93 buf[l] = '\0';
94 if (ndays[i].name != NULL)
95 free(ndays[i].name);
96 if ((ndays[i].name = strdup(buf)) == NULL)
97 errx(EXIT_FAILURE, "cannot allocate memory");
98 ndays[i].len = strlen(buf);
100 strftime(buf, sizeof(buf), "%A", &tm);
101 for (l = strlen(buf);
102 l > 0 && isspace((unsigned char)buf[l - 1]);
103 l--)
105 buf[l] = '\0';
106 if (fndays[i].name != NULL)
107 free(fndays[i].name);
108 if ((fndays[i].name = strdup(buf)) == NULL)
109 errx(EXIT_FAILURE, "cannot allocate memory");
110 fndays[i].len = strlen(buf);
113 for (i = 0; i < 12; i++) {
114 tm.tm_mon = i;
115 strftime(buf, sizeof(buf), "%b", &tm);
116 for (l = strlen(buf);
117 l > 0 && isspace((unsigned char)buf[l - 1]);
118 l--)
120 buf[l] = '\0';
121 if (nmonths[i].name != NULL)
122 free(nmonths[i].name);
123 if ((nmonths[i].name = strdup(buf)) == NULL)
124 errx(EXIT_FAILURE, "cannot allocate memory");
125 nmonths[i].len = strlen(buf);
127 strftime(buf, sizeof(buf), "%B", &tm);
128 for (l = strlen(buf);
129 l > 0 && isspace((unsigned char)buf[l - 1]);
130 l--)
132 buf[l] = '\0';
133 if (fnmonths[i].name != NULL)
134 free(fnmonths[i].name);
135 if ((fnmonths[i].name = strdup(buf)) == NULL)
136 errx(EXIT_FAILURE, "cannot allocate memory");
137 fnmonths[i].len = strlen(buf);
141 void
142 settime(time_t now)
144 char *oldl, *lbufp;
146 tp = localtime(&now);
147 if (isleap(tp->tm_year + 1900)) {
148 yrdays = 366;
149 cumdays = daytab[1];
150 } else {
151 yrdays = 365;
152 cumdays = daytab[0];
154 /* Friday displays Monday's events */
155 if (f_dayAfter == 0 && f_dayBefore == 0 && Friday != -1)
156 f_dayAfter = tp->tm_wday == Friday ? 3 : 1;
157 header[5].iov_base = dayname;
159 oldl = NULL;
160 lbufp = setlocale(LC_TIME, NULL);
161 if (lbufp != NULL && (oldl = strdup(lbufp)) == NULL)
162 errx(EXIT_FAILURE, "cannot allocate memory");
163 setlocale(LC_TIME, "C");
164 header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
165 setlocale(LC_TIME, (oldl != NULL ? oldl : ""));
166 if (oldl != NULL)
167 free(oldl);
169 setnnames();
172 /* convert Day[/Month][/Year] into unix time (since 1970)
173 * Day: two digits, Month: two digits, Year: digits
175 time_t
176 Mktime(char *dp)
178 time_t t;
179 int d, m, y;
180 struct tm tm;
182 time(&t);
183 tp = localtime(&t);
185 tm = tm0;
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)) {
191 case 3:
192 if (y > 1900)
193 y -= 1900;
194 tm.tm_year = y;
195 /* FALLTHROUGH */
196 case 2:
197 tm.tm_mon = m - 1;
198 /* FALLTHROUGH */
199 case 1:
200 tm.tm_mday = d;
203 #ifdef DEBUG
204 fprintf(stderr, "Mktime: %d %d %s\n", (int)mktime(&tm), (int)t,
205 asctime(&tm));
206 #endif
207 return(mktime(&tm));
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(char *endp, int *monthp, int *dayp, int *varp)
223 int day, flags, month = 0, v1, v2;
226 * CONVENTION
228 * Month: 1-12
229 * Monthname: Jan .. Dec
230 * Day: 1-31
231 * Weekday: Mon-Sun
235 flags = 0;
237 /* read first field */
238 /* didn't recognize anything, skip it */
239 if (!(v1 = getfield(endp, &endp, &flags)))
240 return (0);
242 /* Easter or Easter depending days */
243 if (flags & F_EASTER)
244 day = v1 - 1; /* days since January 1 [0-365] */
247 * 1. {Weekday,Day} XYZ ...
249 * where Day is > 12
251 else if (flags & F_ISDAY || v1 > 12) {
253 /* found a day; day: 1-31 or weekday: 1-7 */
254 day = v1;
256 /* {Day,Weekday} {Month,Monthname} ... */
257 /* if no recognizable month, assume just a day alone
258 * in other words, find month or use current month */
259 if (!(month = getfield(endp, &endp, &flags)))
260 month = tp->tm_mon + 1;
263 /* 2. {Monthname} XYZ ... */
264 else if (flags & F_ISMONTH) {
265 month = v1;
267 /* Monthname {day,weekday} */
268 /* if no recognizable day, assume the first day in month */
269 if (!(day = getfield(endp, &endp, &flags)))
270 day = 1;
273 /* Hm ... */
274 else {
275 v2 = getfield(endp, &endp, &flags);
278 * {Day} {Monthname} ...
279 * where Day <= 12
281 if (flags & F_ISMONTH) {
282 day = v1;
283 month = v2;
284 *varp = 0;
287 /* {Month} {Weekday,Day} ... */
288 else {
289 /* F_ISDAY set, v2 > 12, or no way to tell */
290 month = v1;
291 /* if no recognizable day, assume the first */
292 day = v2 ? v2 : 1;
293 *varp = 0;
297 /* convert Weekday into *next* Day,
298 * e.g.: 'Sunday' -> 22
299 * 'SundayLast' -> ??
301 if (flags & F_ISDAY) {
302 #ifdef DEBUG
303 fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
304 #endif
306 *varp = 1;
307 /* variable weekday, SundayLast, MondayFirst ... */
308 if (day < 0 || day >= 10) {
310 /* negative offset; last, -4 .. -1 */
311 if (day < 0) {
312 v1 = day/10 - 1; /* offset -4 ... -1 */
313 day = 10 + (day % 10); /* day 1 ... 7 */
315 /* day, eg '22nd' */
316 v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
318 /* (month length - day) / 7 + 1 */
319 if (cumdays[month+1] - cumdays[month] >= v2
320 && ((int)((cumdays[month+1] -
321 cumdays[month] - v2) / 7) + 1) == -v1)
322 /* bingo ! */
323 day = v2;
325 /* set to yesterday */
326 else {
327 day = tp->tm_mday - 1;
328 if (day == 0)
329 return (0);
333 /* first, second ... +1 ... +5 */
334 else {
335 v1 = day/10; /* offset: +1 (first Sunday) ... */
336 day = day % 10;
338 /* day, eg '22th' */
339 v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
341 /* Hurrah! matched */
342 if (((v2 - 1 + 7) / 7) == v1)
343 day = v2;
345 /* set to yesterday */
346 else {
347 day = tp->tm_mday - 1;
348 if (day == 0)
349 return (0);
354 /* wired */
355 else {
356 day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
357 *varp = 1;
361 if (!(flags & F_EASTER)) {
362 if (day + cumdays[month] > cumdays[month + 1]) { /* off end of month */
363 day -= (cumdays[month + 1] - cumdays[month]); /* adjust */
364 if (++month > 12) /* next year */
365 month = 1;
367 *monthp = month;
368 *dayp = day;
369 day = cumdays[month] + day;
371 else {
372 for (v1 = 0; day > cumdays[v1]; v1++)
374 *monthp = v1 - 1;
375 *dayp = day - cumdays[v1 - 1];
376 *varp = 1;
379 #ifdef DEBUG
380 fprintf(stderr, "day2: day %d(%d-%d) yday %d\n", *dayp, day,
381 cumdays[month], tp->tm_yday);
382 #endif
384 /* When days before or days after is specified */
385 /* no year rollover */
386 if (day >= tp->tm_yday - f_dayBefore &&
387 day <= tp->tm_yday + f_dayAfter)
388 return(1);
390 /* next year */
391 if (tp->tm_yday + f_dayAfter >= yrdays) {
392 int end = tp->tm_yday + f_dayAfter - yrdays;
393 if (day <= end)
394 return(1);
397 /* previous year */
398 if (tp->tm_yday - f_dayBefore < 0) {
399 int before = yrdays + (tp->tm_yday - f_dayBefore);
400 if (day >= before)
401 return(1);
404 return(0);
409 getmonth(char *s)
411 const char **p;
412 struct fixs *n;
414 for (n = fnmonths; n->name; ++n)
415 if (!strncasecmp(s, n->name, n->len))
416 return ((n - fnmonths) + 1);
417 for (n = nmonths; n->name; ++n)
418 if (!strncasecmp(s, n->name, n->len))
419 return ((n - nmonths) + 1);
420 for (p = months; *p; ++p)
421 if (!strncasecmp(s, *p, 3))
422 return ((p - months) + 1);
423 return(0);
428 getday(char *s)
430 const char **p;
431 struct fixs *n;
433 for (n = fndays; n->name; ++n)
434 if (!strncasecmp(s, n->name, n->len))
435 return((n - fndays) + 1);
436 for (n = ndays; n->name; ++n)
437 if (!strncasecmp(s, n->name, n->len))
438 return((n - ndays) + 1);
439 for (p = days; *p; ++p)
440 if (!strncasecmp(s, *p, 3))
441 return((p - days) + 1);
442 return(0);
445 /* return offset for variable weekdays
446 * -1 -> last weekday in month
447 * +1 -> first weekday in month
448 * ... etc ...
451 getdayvar(char *s)
453 int offs;
456 offs = strlen(s);
459 /* Sun+1 or Wednesday-2
460 * ^ ^ */
462 /* fprintf(stderr, "x: %s %s %d\n", s, s + offs - 2, offs); */
463 switch(*(s + offs - 2)) {
464 case '-':
465 return(-(atoi(s + offs - 1)));
466 case '+':
467 return(atoi(s + offs - 1));
472 * some aliases: last, first, second, third, fourth
475 /* last */
476 if (offs > 4 && !strcasecmp(s + offs - 4, "last"))
477 return(-1);
478 else if (offs > 5 && !strcasecmp(s + offs - 5, "first"))
479 return(+1);
480 else if (offs > 6 && !strcasecmp(s + offs - 6, "second"))
481 return(+2);
482 else if (offs > 5 && !strcasecmp(s + offs - 5, "third"))
483 return(+3);
484 else if (offs > 6 && !strcasecmp(s + offs - 6, "fourth"))
485 return(+4);
488 /* no offset detected */
489 return(0);