/* Algorithm to compute the 4th Sunday in Advent (ie the last Sunday before Christmas Day) */ static time_t get4AdventDate(int year) { time_t day; struct tm *t; day = mkday(year, 12, 24); t = localtime(&day); day -= t->tm_wday * 86400; return day; }
int main() { begin = NULL; time_t now; time(&now); day* d = mkday(&now); insertday(d); printf("%s\n", ctime( &now)); printlist(); }
/* * Algorithm for calculating the date of Easter Sunday * (Meeus/Jones/Butcher Gregorian algorithm) * For reference, see http://en.wikipedia.org/wiki/Computus */ static time_t getEasterDate(int year) { time_t day; int Y = year+1900; int a = Y % 19; int b = Y / 100; int c = Y % 100; int d = b / 4; int e = b % 4; int f = (b + 8) / 25; int g = (b - f + 1) / 3; int h = (19 * a + b - d - g + 15) % 30; int i = c / 4; int k = c % 4; int L = (32 + 2 * e + 2 * i - h - k) % 7; int m = (a + 11 * h + 22 * L) / 451; day = mkday(year, (h + L - 7 * m + 114) / 31, ((h + L - 7 * m + 114) % 31) + 1); return day; }
static void add_holiday(char *key, int year, holiday_t *newhol) { int isOK = 0; struct tm *t; time_t day; holiday_t *newitem; xtreePos_t handle; holidayset_t *hset; switch (newhol->holtype) { case HOL_ABSOLUTE: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <=31) && (!newhol->year || (newhol->year == year)) ); if (!isOK) break; day = mkday(year, newhol->month, newhol->day); t = localtime(&day); newhol->yday = t->tm_yday; break; case HOL_EASTER: isOK = (newhol->month == 0); if (!isOK) break; day = getEasterDate(year); t = localtime(&day); newhol->yday = t->tm_yday + newhol->day; break; case HOL_ADVENT: isOK = (newhol->month == 0); if (!isOK) break; day = get4AdventDate(year); t = localtime(&day); newhol->yday = t->tm_yday + newhol->day; break; case HOL_MON: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 5) ); if (!isOK) break; newhol->yday = getnumberedweekday(1, newhol->day, newhol->month, year); break; case HOL_TUE: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 5) ); if (!isOK) break; newhol->yday = getnumberedweekday(2, newhol->day, newhol->month, year); break; case HOL_WED: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 5) ); if (!isOK) break; newhol->yday = getnumberedweekday(3, newhol->day, newhol->month, year); break; case HOL_THU: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 5) ); if (!isOK) break; newhol->yday = getnumberedweekday(4, newhol->day, newhol->month, year); break; case HOL_FRI: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 5) ); if (!isOK) break; newhol->yday = getnumberedweekday(5, newhol->day, newhol->month, year); break; case HOL_SAT: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 5) ); if (!isOK) break; newhol->yday = getnumberedweekday(6, newhol->day, newhol->month, year); break; case HOL_SUN: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 5) ); if (!isOK) break; newhol->yday = getnumberedweekday(0, newhol->day, newhol->month, year); break; case HOL_MON_AFTER: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 31) ); if (!isOK) break; newhol->yday = getweekdayafter(1, newhol->day, newhol->month, year); break; case HOL_TUE_AFTER: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 31) ); if (!isOK) break; newhol->yday = getweekdayafter(2, newhol->day, newhol->month, year); break; case HOL_WED_AFTER: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 31) ); if (!isOK) break; newhol->yday = getweekdayafter(3, newhol->day, newhol->month, year); break; case HOL_THU_AFTER: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 31) ); if (!isOK) break; newhol->yday = getweekdayafter(4, newhol->day, newhol->month, year); break; case HOL_FRI_AFTER: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 31) ); if (!isOK) break; newhol->yday = getweekdayafter(5, newhol->day, newhol->month, year); break; case HOL_SAT_AFTER: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 31) ); if (!isOK) break; newhol->yday = getweekdayafter(6, newhol->day, newhol->month, year); break; case HOL_SUN_AFTER: isOK = ( (newhol->month >= 1 && newhol->month <=12) && (newhol->day >=1 && newhol->day <= 31) ); if (!isOK) break; newhol->yday = getweekdayafter(0, newhol->day, newhol->month, year); break; } if (!isOK) { errprintf("Error in holiday definition %s\n", newhol->desc); return; } newitem = (holiday_t *)calloc(1, sizeof(holiday_t)); newitem->holtype = newhol->holtype; newitem->day = newhol->day; newitem->month = newhol->month; newitem->desc = strdup(newhol->desc); newitem->yday = newhol->yday; handle = xtreeFind(holidays, key); if (handle == xtreeEnd(holidays)) { hset = (holidayset_t *)calloc(1, sizeof(holidayset_t)); hset->key = strdup(key); xtreeAdd(holidays, hset->key, hset); } else { hset = (holidayset_t *)xtreeData(holidays, handle); } if (hset->head == NULL) { hset->head = hset->tail = newitem; } else { hset->tail->next = newitem; hset->tail = hset->tail->next; } }