version 1.3, 1998/02/23 05:48:42 |
version 1.34, 2016/09/14 15:09:46 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
* 2. Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
* 3. All advertising materials mentioning features or use of this software |
* 3. Neither the name of the University nor the names of its contributors |
* must display the following acknowledgement: |
|
* This product includes software developed by the University of |
|
* California, Berkeley and its contributors. |
|
* 4. Neither the name of the University nor the names of its contributors |
|
* may be used to endorse or promote products derived from this software |
* may be used to endorse or promote products derived from this software |
* without specific prior written permission. |
* without specific prior written permission. |
* |
* |
|
|
* SUCH DAMAGE. |
* SUCH DAMAGE. |
*/ |
*/ |
|
|
#ifndef lint |
|
static const char copyright[] = |
|
"@(#) Copyright (c) 1989, 1993\n\ |
|
The Regents of the University of California. All rights reserved.\n"; |
|
#endif /* not lint */ |
|
|
|
#ifndef lint |
|
#if 0 |
|
static const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; |
|
#else |
|
static char rcsid[] = "$OpenBSD$"; |
|
#endif |
|
#endif /* not lint */ |
|
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/uio.h> |
#include <sys/uio.h> |
|
|
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <time.h> |
#include <time.h> |
#include <tzfile.h> |
|
|
|
#include "pathnames.h" |
#include "pathnames.h" |
#include "calendar.h" |
#include "calendar.h" |
|
|
|
extern struct iovec header[]; |
|
|
|
#define WEEKLY 1 |
|
#define MONTHLY 2 |
|
#define YEARLY 3 |
|
|
struct tm *tp; |
struct tm *tp; |
int *cumdays, offset, yrdays; |
int *cumdays, offset; |
char dayname[10]; |
char dayname[10]; |
|
enum calendars calendar; |
|
u_long julian; |
|
|
|
|
/* 1-based month, 0-based days, cumulative */ |
/* 1-based month, 0-based days, cumulative */ |
|
|
static struct fixs fnmonths[13]; /* full national months names */ |
static struct fixs fnmonths[13]; /* full national months names */ |
static struct fixs nmonths[13]; /* short national month names */ |
static struct fixs nmonths[13]; /* short national month names */ |
|
|
|
void |
|
fill_print_date(struct match *m, struct tm *tm) |
|
{ |
|
if (strftime(m->print_date, sizeof(m->print_date), |
|
daynames ? "%a %b %d" : "%b %d", tm) == 0) |
|
m->print_date[sizeof(m->print_date) - 1] = '\0'; |
|
} |
|
|
void setnnames(void) |
void |
|
setnnames(void) |
{ |
{ |
char buf[80]; |
char buf[80]; |
int i, l; |
int i, l; |
|
|
for (i = 0; i < 7; i++) { |
for (i = 0; i < 7; i++) { |
tm.tm_wday = i; |
tm.tm_wday = i; |
l = strftime(buf, sizeof(buf), "%a", &tm); |
l = strftime(buf, sizeof(buf), "%a", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (ndays[i].name != NULL) |
free(ndays[i].name); |
free(ndays[i].name); |
|
if ((ndays[i].name = strdup(buf)) == NULL) |
if ((ndays[i].name = strdup(buf)) == NULL) |
errx(1, "cannot allocate memory"); |
err(1, NULL); |
ndays[i].len = strlen(buf); |
ndays[i].len = strlen(buf); |
|
|
l = strftime(buf, sizeof(buf), "%A", &tm); |
l = strftime(buf, sizeof(buf), "%A", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (fndays[i].name != NULL) |
free(fndays[i].name); |
free(fndays[i].name); |
|
if ((fndays[i].name = strdup(buf)) == NULL) |
if ((fndays[i].name = strdup(buf)) == NULL) |
errx(1, "cannot allocate memory"); |
err(1, NULL); |
fndays[i].len = strlen(buf); |
fndays[i].len = strlen(buf); |
} |
} |
|
|
for (i = 0; i < 12; i++) { |
for (i = 0; i < 12; i++) { |
tm.tm_mon = i; |
tm.tm_mon = i; |
l = strftime(buf, sizeof(buf), "%b", &tm); |
l = strftime(buf, sizeof(buf), "%b", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (nmonths[i].name != NULL) |
free(nmonths[i].name); |
free(nmonths[i].name); |
|
if ((nmonths[i].name = strdup(buf)) == NULL) |
if ((nmonths[i].name = strdup(buf)) == NULL) |
errx(1, "cannot allocate memory"); |
err(1, NULL); |
nmonths[i].len = strlen(buf); |
nmonths[i].len = strlen(buf); |
|
|
l = strftime(buf, sizeof(buf), "%B", &tm); |
l = strftime(buf, sizeof(buf), "%B", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (fnmonths[i].name != NULL) |
free(fnmonths[i].name); |
free(fnmonths[i].name); |
|
if ((fnmonths[i].name = strdup(buf)) == NULL) |
if ((fnmonths[i].name = strdup(buf)) == NULL) |
errx(1, "cannot allocate memory"); |
err(1, NULL); |
fnmonths[i].len = strlen(buf); |
fnmonths[i].len = strlen(buf); |
} |
} |
|
/* Hardwired special events */ |
|
spev[0].name = strdup(PESACH); |
|
spev[0].nlen = PESACHLEN; |
|
spev[0].getev = pesach; |
|
spev[1].name = strdup(EASTER); |
|
spev[1].nlen = EASTERNAMELEN; |
|
spev[1].getev = easter; |
|
spev[2].name = strdup(PASKHA); |
|
spev[2].nlen = PASKHALEN; |
|
spev[2].getev = paskha; |
|
for (i = 0; i < NUMEV; i++) { |
|
if (spev[i].name == NULL) |
|
err(1, NULL); |
|
spev[i].uname = NULL; |
|
} |
} |
} |
|
|
void |
void |
settime(now) |
settime(time_t *now) |
time_t now; |
|
{ |
{ |
tp = localtime(&now); |
tp = localtime(now); |
if (isleap(tp->tm_year + 1900)) { |
tp->tm_sec = 0; |
yrdays = DAYSPERLYEAR; |
tp->tm_min = 0; |
|
/* Avoid getting caught by a timezone shift; set time to noon */ |
|
tp->tm_isdst = 0; |
|
tp->tm_hour = 12; |
|
*now = mktime(tp); |
|
if (isleap(tp->tm_year + 1900)) |
cumdays = daytab[1]; |
cumdays = daytab[1]; |
} else { |
else |
yrdays = DAYSPERNYEAR; |
|
cumdays = daytab[0]; |
cumdays = daytab[0]; |
} |
|
/* Friday displays Monday's events */ |
/* Friday displays Monday's events */ |
offset = tp->tm_wday == 5 ? 3 : 1; |
offset = tp->tm_wday == 5 ? 3 : 1; |
|
if (f_SetdayAfter) |
|
offset = 0; /* Except not when range is set explicitly */ |
header[5].iov_base = dayname; |
header[5].iov_base = dayname; |
|
|
(void) setlocale(LC_TIME, "C"); |
(void) setlocale(LC_TIME, "C"); |
|
|
/* convert [Year][Month]Day into unix time (since 1970) |
/* convert [Year][Month]Day into unix time (since 1970) |
* Year: two or four digits, Month: two digits, Day: two digits |
* Year: two or four digits, Month: two digits, Day: two digits |
*/ |
*/ |
time_t Mktime (date) |
time_t |
char *date; |
Mktime(char *date) |
{ |
{ |
time_t t; |
time_t t; |
char save; |
int len; |
int len; |
struct tm tm; |
struct tm tm; |
|
|
|
(void)time(&t); |
(void)time(&t); |
tp = localtime(&t); |
tp = localtime(&t); |
|
|
len = strlen(date); |
len = strlen(date); |
if (len < 2) |
if (len < 2) |
return((time_t)-1); |
return((time_t)-1); |
tm.tm_sec = 0; |
bzero(&tm, sizeof tm); |
tm.tm_min = 0; |
tm.tm_sec = 0; |
tm.tm_hour = 0; |
tm.tm_min = 0; |
tm.tm_wday = 0; |
/* Avoid getting caught by a timezone shift; set time to noon */ |
tm.tm_mday = tp->tm_mday; |
tm.tm_isdst = 0; |
tm.tm_mon = tp->tm_mon; |
tm.tm_hour = 12; |
tm.tm_year = tp->tm_year; |
tm.tm_wday = 0; |
|
tm.tm_mday = tp->tm_mday; |
|
tm.tm_mon = tp->tm_mon; |
|
tm.tm_year = tp->tm_year; |
|
|
/* Day */ |
/* Day */ |
tm.tm_mday = atoi(date + len - 2); |
tm.tm_mday = atoi(date + len - 2); |
|
|
/* Month */ |
/* Month */ |
if (len >= 4) { |
if (len >= 4) { |
*(date + len - 2) = '\0'; |
*(date + len - 2) = '\0'; |
tm.tm_mon = atoi(date + len - 4) - 1; |
tm.tm_mon = atoi(date + len - 4) - 1; |
} |
} |
|
|
/* Year */ |
/* Year */ |
if (len >= 7) { |
if (len >= 6) { |
*(date + len - 4) = '\0'; |
*(date + len - 4) = '\0'; |
tm.tm_year = atoi(date); |
tm.tm_year = atoi(date); |
|
|
/* tm_year up 1900 ... */ |
if (tm.tm_year < 69) /* Y2K */ |
if (tm.tm_year > 1900) |
tm.tm_year += 100; |
tm.tm_year -= 1900; |
else if (tm.tm_year > 1900) |
} |
tm.tm_year -= 1900; |
|
} |
|
|
#if DEBUG |
#if DEBUG |
printf("Mktime: %d %d %d %s\n", (int)mktime(&tm), (int)t, len, |
printf("Mktime: %d %lld %d %s\n", (int)mktime(&tm), (long long)t, len, |
asctime(&tm)); |
asctime(&tm)); |
#endif |
#endif |
return(mktime(&tm)); |
return(mktime(&tm)); |
} |
} |
|
|
|
static void |
|
adjust_calendar(int *day, int *month) |
|
{ |
|
switch (calendar) { |
|
case GREGORIAN: |
|
break; |
|
|
|
case JULIAN: |
|
*day += julian; |
|
if (*day > (cumdays[*month + 1] - cumdays[*month])) { |
|
*day -= (cumdays[*month + 1] - cumdays[*month]); |
|
if (++*month > 12) |
|
*month = 1; |
|
} |
|
break; |
|
case LUNAR: |
|
break; |
|
} |
|
} |
|
|
/* |
/* |
* Possible date formats include any combination of: |
* Possible date formats include any combination of: |
* 3-charmonth (January, Jan, Jan) |
* 3-charmonth (January, Jan, Jan) |
* 3-charweekday (Friday, Monday, mon.) |
* 3-charweekday (Friday, Monday, mon.) |
* numeric month or day (1, 2, 04) |
* numeric month or day (1, 2, 04) |
* |
* |
* Any character may separate them, or they may not be separated. Any line, |
* Any character except \t or '*' may separate them, or they may not be |
* following a line that is matched, that starts with "whitespace", is shown |
* separated. Any line following a line that is matched, that starts |
* along with the matched line. |
* with \t, is shown along with the matched line. |
*/ |
*/ |
int |
struct match * |
isnow(endp, monthp, dayp, varp) |
isnow(char *endp, int bodun) |
char *endp; |
|
int *monthp; |
|
int *dayp; |
|
int *varp; |
|
{ |
{ |
int day, flags = 0, month = 0, v1, v2; |
int day = 0, flags = 0, month = 0, v1, v2, i; |
|
int monthp, dayp, varp = 0; |
|
struct match *matches = NULL, *tmp, *tmp2; |
|
int interval = YEARLY; /* how frequently the event repeats. */ |
|
int vwd = 0; /* Variable weekday */ |
|
time_t tdiff, ttmp; |
|
struct tm tmtmp; |
|
|
/* |
/* |
* CONVENTION |
* CONVENTION |
|
|
/* read first field */ |
/* read first field */ |
/* didn't recognize anything, skip it */ |
/* didn't recognize anything, skip it */ |
if (!(v1 = getfield(endp, &endp, &flags))) |
if (!(v1 = getfield(endp, &endp, &flags))) |
return (0); |
return (NULL); |
|
|
|
/* adjust bodun rate */ |
|
if (bodun && !bodun_always) |
|
bodun = !arc4random_uniform(3); |
|
|
/* Easter or Easter depending days */ |
/* Easter or Easter depending days */ |
if (flags & F_EASTER) |
if (flags & F_SPECIAL) |
day = v1 - 1; /* days since January 1 [0-365] */ |
vwd = v1; |
|
|
/* |
/* |
* 1. {Weekday,Day} XYZ ... |
* 1. {Weekday,Day} XYZ ... |
|
|
*/ |
*/ |
else if (flags & F_ISDAY || v1 > 12) { |
else if (flags & F_ISDAY || v1 > 12) { |
|
|
/* found a day; day: 1-31 or weekday: 1-7 */ |
/* found a day; day: 13-31 or weekday: 1-7 */ |
day = v1; |
day = v1; |
|
|
/* {Day,Weekday} {Month,Monthname} ... */ |
/* {Day,Weekday} {Month,Monthname} ... */ |
/* if no recognizable month, assume just a day alone |
/* if no recognizable month, assume just a day alone -- this is |
* in other words, find month or use current month */ |
* very unlikely and can only happen after the first 12 days. |
if (!(month = getfield(endp, &endp, &flags))) |
* --find month or use current month */ |
|
if (!(month = getfield(endp, &endp, &flags))) { |
month = tp->tm_mon + 1; |
month = tp->tm_mon + 1; |
|
/* F_ISDAY is set only if a weekday was spelled out */ |
|
/* F_ISDAY must be set if 0 < day < 8 */ |
|
if ((day <= 7) && (day >= 1)) |
|
interval = WEEKLY; |
|
else |
|
interval = MONTHLY; |
|
} else if ((day <= 7) && (day >= 1)) |
|
day += 10; |
|
/* it's a weekday; make it the first one of the month */ |
|
if (month == -1) { |
|
month = tp->tm_mon + 1; |
|
interval = MONTHLY; |
|
} else if (calendar) |
|
adjust_calendar(&day, &month); |
|
if ((month > 12) || (month < 1)) |
|
return (NULL); |
} |
} |
|
|
/* 2. {Monthname} XYZ ... */ |
/* 2. {Monthname} XYZ ... */ |
else if (flags & F_ISMONTH) { |
else if (flags & F_ISMONTH) { |
month = v1; |
month = v1; |
|
if (month == -1) { |
|
month = tp->tm_mon + 1; |
|
interval = MONTHLY; |
|
} |
/* Monthname {day,weekday} */ |
/* Monthname {day,weekday} */ |
/* if no recognizable day, assume the first day in month */ |
/* if no recognizable day, assume the first day in month */ |
if (!(day = getfield(endp, &endp, &flags))) |
if (!(day = getfield(endp, &endp, &flags))) |
day = 1; |
day = 1; |
|
/* If a weekday was spelled out without an ordering, |
|
* assume the first of that day in the month */ |
|
if ((flags & F_ISDAY)) { |
|
if ((day >= 1) && (day <=7)) |
|
day += 10; |
|
} else if (calendar) |
|
adjust_calendar(&day, &month); |
} |
} |
|
|
/* Hm ... */ |
/* Hm ... */ |
|
|
if (flags & F_ISMONTH) { |
if (flags & F_ISMONTH) { |
day = v1; |
day = v1; |
month = v2; |
month = v2; |
*varp = 0; |
if (month == -1) { |
|
month = tp->tm_mon + 1; |
|
interval = MONTHLY; |
|
} else if (calendar) |
|
adjust_calendar(&day, &month); |
} |
} |
|
|
/* {Month} {Weekday,Day} ... */ |
/* {Month} {Weekday,Day} ... */ |
|
|
month = v1; |
month = v1; |
/* if no recognizable day, assume the first */ |
/* if no recognizable day, assume the first */ |
day = v2 ? v2 : 1; |
day = v2 ? v2 : 1; |
*varp = 0; |
if ((flags & F_ISDAY)) { |
|
if ((day >= 1) && (day <= 7)) |
|
day += 10; |
|
} else |
|
adjust_calendar(&day, &month); |
} |
} |
} |
} |
|
|
/* convert Weekday into *next* Day, |
/* convert Weekday into *next* Day, |
* e.g.: 'Sunday' -> 22 |
* e.g.: 'Sunday' -> 22 |
* 'SunayLast' -> ?? |
* 'SundayLast' -> ?? |
*/ |
*/ |
if (flags & F_ISDAY) { |
if (flags & F_ISDAY) { |
#if DEBUG |
#if DEBUG |
fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month); |
fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month); |
#endif |
#endif |
|
|
*varp = 1; |
varp = 1; |
/* variable weekday, SundayLast, MondayFirst ... */ |
/* variable weekday, SundayLast, MondayFirst ... */ |
if (day < 0 || day >= 10) { |
if (day < 0 || day >= 10) |
|
vwd = day; |
/* negative offset; last, -4 .. -1 */ |
else { |
if (day < 0) { |
day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); |
v1 = day/10 - 1; /* offset -4 ... -1 */ |
interval = WEEKLY; |
day = 10 + (day % 10); /* day 1 ... 7 */ |
|
|
|
/* day, eg '22th' */ |
|
v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); |
|
|
|
/* (month length - day) / 7 + 1 */ |
|
if (((int)((cumdays[month+1] - |
|
cumdays[month] - v2) / 7) + 1) == -v1) |
|
/* bingo ! */ |
|
day = v2; |
|
|
|
/* set to yesterday */ |
|
else |
|
day = tp->tm_mday - 1; |
|
} |
} |
|
} else |
|
/* Check for silliness. Note we still catch Feb 29 */ |
|
if (!(flags & F_SPECIAL) && |
|
(day > (cumdays[month + 1] - cumdays[month]) || day < 1)) { |
|
if (!((month == 2 && day == 29) || |
|
(interval == MONTHLY && day <= 31))) |
|
return (NULL); |
|
} |
|
|
/* first, second ... +1 ... +5 */ |
if (!(flags & F_SPECIAL)) { |
else { |
monthp = month; |
v1 = day/10; /* offset: +1 (first Sunday) ... */ |
dayp = day; |
day = day % 10; |
day = cumdays[month] + day; |
|
#if DEBUG |
|
fprintf(stderr, "day2: day %d(%d) yday %d\n", dayp, day, tp->tm_yday); |
|
#endif |
|
/* Speed up processing for the most common situation: yearly events |
|
* when the interval being checked is less than a month or so (this |
|
* could be less than a year, but then we have to start worrying about |
|
* leap years). Only one event can match, and it's easy to find. |
|
* Note we can't check special events, because they can wander widely. |
|
*/ |
|
if (((v1 = offset + f_dayAfter) < 50) && (interval == YEARLY)) { |
|
memcpy(&tmtmp, tp, sizeof(struct tm)); |
|
tmtmp.tm_mday = dayp; |
|
tmtmp.tm_mon = monthp - 1; |
|
if (vwd) { |
|
/* We want the event next year if it's late now |
|
* this year. The 50-day limit means we don't have to |
|
* worry if next year is or isn't a leap year. |
|
*/ |
|
if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1) |
|
variable_weekday(&vwd, tmtmp.tm_mon + 1, |
|
tmtmp.tm_year + 1900 + 1); |
|
else |
|
variable_weekday(&vwd, tmtmp.tm_mon + 1, |
|
tmtmp.tm_year + 1900); |
|
day = cumdays[tmtmp.tm_mon + 1] + vwd; |
|
tmtmp.tm_mday = vwd; |
|
} |
|
v2 = day - tp->tm_yday; |
|
if ((v2 > v1) || (v2 < 0)) { |
|
if ((v2 += isleap(tp->tm_year + 1900) ? 366 : 365) |
|
<= v1) |
|
tmtmp.tm_year++; |
|
else if(!bodun || (day - tp->tm_yday) != -1) |
|
return(NULL); |
|
} |
|
if ((tmp = malloc(sizeof(struct match))) == NULL) |
|
err(1, NULL); |
|
|
/* day, eg '22th' */ |
if (bodun && (day - tp->tm_yday) == -1) { |
v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); |
tmp->when = f_time - 1 * SECSPERDAY; |
|
tmtmp.tm_mday++; |
|
tmp->bodun = 1; |
|
} else { |
|
tmp->when = f_time + v2 * SECSPERDAY; |
|
tmp->bodun = 0; |
|
} |
|
|
/* Hurrah! matched */ |
(void)mktime(&tmtmp); |
if ( ((v2 - 1 + 7) / 7) == v1 ) |
fill_print_date(tmp, &tmtmp); |
day = v2; |
tmp->var = varp; |
|
tmp->next = NULL; |
/* set to yesterday */ |
return(tmp); |
else |
|
day = tp->tm_mday - 1; |
|
} |
} |
} |
} else { |
|
varp = 1; |
/* wired */ |
/* Set up v1 to the event number and ... */ |
else { |
v1 = vwd % (NUMEV + 1) - 1; |
day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7); |
vwd /= (NUMEV + 1); |
*varp = 1; |
if (v1 < 0) { |
} |
v1 += NUMEV + 1; |
|
vwd--; |
|
} |
|
dayp = monthp = 1; /* Why not */ |
} |
} |
|
|
if (!(flags & F_EASTER)) { |
/* Compare to past and coming instances of the event. The i == 0 part |
*monthp = month; |
* of the loop corresponds to this specific instance. Note that we |
*dayp = day; |
* can leave things sort of higgledy-piggledy since a mktime() happens |
day = cumdays[month] + day; |
* on this before anything gets printed. Also note that even though |
|
* we've effectively gotten rid of f_dayBefore, we still have to check |
|
* the one prior event for situations like "the 31st of every month" |
|
* and "yearly" events which could happen twice in one year but not in |
|
* the next */ |
|
tmp2 = matches; |
|
for (i = -1; i < 2; i++) { |
|
memcpy(&tmtmp, tp, sizeof(struct tm)); |
|
tmtmp.tm_mday = dayp; |
|
tmtmp.tm_mon = month = monthp - 1; |
|
do { |
|
v2 = 0; |
|
switch (interval) { |
|
case WEEKLY: |
|
tmtmp.tm_mday += 7 * i; |
|
break; |
|
case MONTHLY: |
|
month += i; |
|
tmtmp.tm_mon = month; |
|
switch(tmtmp.tm_mon) { |
|
case -1: |
|
tmtmp.tm_mon = month = 11; |
|
tmtmp.tm_year--; |
|
break; |
|
case 12: |
|
tmtmp.tm_mon = month = 0; |
|
tmtmp.tm_year++; |
|
break; |
|
} |
|
if (vwd) { |
|
v1 = vwd; |
|
variable_weekday(&v1, tmtmp.tm_mon + 1, |
|
tmtmp.tm_year + 1900); |
|
tmtmp.tm_mday = v1; |
|
} else |
|
tmtmp.tm_mday = dayp; |
|
break; |
|
case YEARLY: |
|
default: |
|
tmtmp.tm_year += i; |
|
if (flags & F_SPECIAL) { |
|
tmtmp.tm_mon = 0; /* Gee, mktime() is nice */ |
|
tmtmp.tm_mday = spev[v1].getev(tmtmp.tm_year + |
|
1900) + vwd; |
|
} else if (vwd) { |
|
v1 = vwd; |
|
variable_weekday(&v1, tmtmp.tm_mon + 1, |
|
tmtmp.tm_year + 1900); |
|
tmtmp.tm_mday = v1; |
|
} else { |
|
/* Need the following to keep Feb 29 from |
|
* becoming Mar 1 */ |
|
tmtmp.tm_mday = dayp; |
|
tmtmp.tm_mon = monthp - 1; |
|
} |
|
break; |
|
} |
|
/* How many days apart are we */ |
|
if ((ttmp = mktime(&tmtmp)) == -1) |
|
warnx("time out of range: %s", endp); |
|
else { |
|
tdiff = difftime(ttmp, f_time)/ SECSPERDAY; |
|
if (tdiff <= offset + f_dayAfter || |
|
(bodun && tdiff == -1)) { |
|
if (((tmtmp.tm_mon == month) || |
|
(flags & F_SPECIAL) || |
|
(interval == WEEKLY)) && |
|
(tdiff >= 0 || |
|
(bodun && tdiff == -1))) { |
|
if ((tmp = malloc(sizeof(struct match))) == NULL) |
|
err(1, NULL); |
|
tmp->when = ttmp; |
|
fill_print_date(tmp, &tmtmp); |
|
tmp->bodun = bodun && tdiff == -1; |
|
tmp->var = varp; |
|
tmp->next = NULL; |
|
if (tmp2) |
|
tmp2->next = tmp; |
|
else |
|
matches = tmp; |
|
tmp2 = tmp; |
|
v2 = (i == 1) ? 1 : 0; |
|
} |
|
} else |
|
i = 2; /* No point checking in the future */ |
|
} |
|
} while (v2 != 0); |
} |
} |
else { |
return (matches); |
for (v1 = 0; day > cumdays[v1]; v1++) |
|
; |
|
*monthp = v1 - 1; |
|
*dayp = day - cumdays[v1 - 1]; |
|
*varp = 1; |
|
} |
|
|
|
#if DEBUG |
|
fprintf(stderr, "day2: day %d(%d) yday %d\n", *dayp, day, tp->tm_yday); |
|
#endif |
|
/* if today or today + offset days */ |
|
if (day >= tp->tm_yday - f_dayBefore && |
|
day <= tp->tm_yday + offset + f_dayAfter) |
|
return (1); |
|
|
|
/* if number of days left in this year + days to event in next year */ |
|
if (yrdays - tp->tm_yday + day <= offset + f_dayAfter || |
|
/* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */ |
|
tp->tm_yday + day - f_dayBefore < 0 |
|
) |
|
return (1); |
|
return (0); |
|
} |
} |
|
|
|
|
int |
int |
getmonth(s) |
getmonth(char *s) |
register char *s; |
|
{ |
{ |
register char **p; |
char **p; |
struct fixs *n; |
struct fixs *n; |
|
|
for (n = fnmonths; n->name; ++n) |
for (n = fnmonths; n->name; ++n) |
|
|
|
|
|
|
int |
int |
getday(s) |
getday(char *s) |
register char *s; |
|
{ |
{ |
register char **p; |
char **p; |
struct fixs *n; |
struct fixs *n; |
|
|
for (n = fndays; n->name; ++n) |
for (n = fndays; n->name; ++n) |
|
|
* ... etc ... |
* ... etc ... |
*/ |
*/ |
int |
int |
getdayvar(s) |
getdayvar(char *s) |
register char *s; |
|
{ |
{ |
register int offset; |
int offset; |
|
|
|
|
offset = strlen(s); |
offset = strlen(s); |
|
|
|
|
/* Sun+1 or Wednesday-2 |
/* Sun+1 or Wednesday-2 |
* ^ ^ */ |
* ^ ^ */ |
|
|
/* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */ |
/* printf ("x: %s %s %d\n", s, s + offset - 2, offset); */ |
switch(*(s + offset - 2)) { |
switch(*(s + offset - 2)) { |
case '-': |
case '-': |
return(-(atoi(s + offset - 1))); |
|
break; |
|
case '+': |
case '+': |
return(atoi(s + offset - 1)); |
return(atoi(s + offset - 2)); |
break; |
break; |
} |
} |
|
|
|
|
/* |
/* |
* some aliases: last, first, second, third, fourth |
* some aliases: last, first, second, third, fourth |
*/ |
*/ |
|
|
else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth")) |
else if (offset > 6 && !strcasecmp(s + offset - 6, "fourth")) |
return(+4); |
return(+4); |
|
|
|
|
/* no offset detected */ |
/* no offset detected */ |
return(0); |
return(0); |
|
} |
|
|
|
|
|
int |
|
foy(int year) |
|
{ |
|
/* 0-6; what weekday Jan 1 is */ |
|
year--; |
|
return ((1 - year/100 + year/400 + (int)(365.25 * year)) % 7); |
|
} |
|
|
|
|
|
|
|
void |
|
variable_weekday(int *day, int month, int year) |
|
{ |
|
int v1, v2; |
|
int *cumdays; |
|
int day1; |
|
|
|
if (isleap(year)) |
|
cumdays = daytab[1]; |
|
else |
|
cumdays = daytab[0]; |
|
day1 = foy(year); |
|
/* negative offset; last, -4 .. -1 */ |
|
if (*day < 0) { |
|
v1 = *day/10 - 1; /* offset -4 ... -1 */ |
|
*day = 10 + (*day % 10); /* day 1 ... 7 */ |
|
|
|
/* which weekday the end of the month is (1-7) */ |
|
v2 = (cumdays[month + 1] + day1) % 7 + 1; |
|
|
|
/* and subtract enough days */ |
|
*day = cumdays[month + 1] - cumdays[month] + |
|
(v1 + 1) * 7 - (v2 - *day + 7) % 7; |
|
#if DEBUG |
|
fprintf(stderr, "\nMonth %d ends on weekday %d\n", month, v2); |
|
#endif |
|
} |
|
|
|
/* first, second ... +1 ... +5 */ |
|
else { |
|
v1 = *day/10; /* offset */ |
|
*day = *day % 10; |
|
|
|
/* which weekday the first of the month is (1-7) */ |
|
v2 = (cumdays[month] + 1 + day1) % 7 + 1; |
|
|
|
/* and add enough days */ |
|
*day = 1 + (v1 - 1) * 7 + (*day - v2 + 7) % 7; |
|
#if DEBUG |
|
fprintf(stderr, "\nMonth %d starts on weekday %d\n", month, v2); |
|
#endif |
|
} |
} |
} |