#include "entry.h" #include static time_t rt; /* super inefficient, but should best-possible avoid any weird leap * years/seconds/dst-problems/other future changes */ time_t* entry_is_active(struct entry_s *e, time_t now) { struct tm t, ttmp, *tmp; time_t rttmp, now_plus_warn, now_minus_stay; assert(e != NULL); tmp = localtime(&now); if (tmp == NULL) return NULL; t = *tmp; ttmp = t; t.tm_year += e->warn.year; t.tm_mon += e->warn.month; t.tm_mday += e->warn.day; t.tm_hour += e->warn.hour; t.tm_min += e->warn.minute; t.tm_sec += e->warn.second; now_plus_warn = mktime(&t); ttmp.tm_year -= e->stay.year; ttmp.tm_mon -= e->stay.month; ttmp.tm_mday -= e->stay.day; ttmp.tm_hour -= e->stay.hour; ttmp.tm_min -= e->stay.minute; ttmp.tm_sec -= e->stay.second; now_minus_stay = mktime(&ttmp); if (now_plus_warn == -1 || now_minus_stay == -1) return NULL; if (e->type == ENTRY_TYPE_ON) { if (e->start >= now_minus_stay && e->start <= now_plus_warn) { rt = e->start; return &rt; } return NULL; } if (e->has_end && now_minus_stay > e->end) return NULL; tmp = localtime( &(e->start) ); if (tmp == NULL) return NULL; t = *tmp; for (rt = e->start;;) { if (rt > now_plus_warn) return NULL; if (rt >= now_minus_stay) return &rt; ttmp = t; t.tm_year += e->every.year; t.tm_mon += e->every.month; t.tm_mday += e->every.day; t.tm_hour += e->every.hour; t.tm_min += e->every.minute; t.tm_sec += e->every.second; rt = mktime(&t); if (rt == -1) { rttmp = mktime(&ttmp); if (rttmp == -1) return NULL; tmp = localtime(&rttmp); if (tmp == NULL) return NULL; t = *tmp; t.tm_year += e->every.year; t.tm_mon += e->every.month; t.tm_mday += e->every.day; t.tm_hour += e->every.hour; t.tm_min += e->every.minute; t.tm_sec += e->every.second; rt = mktime(&t); if (rt == -1) return NULL; } } }