aboutsummaryrefslogtreecommitdiffstats
path: root/src/entry.c
diff options
context:
space:
mode:
authorkatherine <ageha@airen-no-jikken.icu>2019-12-20 09:28:54 -0700
committerkatherine <ageha@airen-no-jikken.icu>2019-12-20 09:28:54 -0700
commit41f73b0a45ba3e63ac33ebdc99f12481f5b7cdab (patch)
treecb863c4372a1386a27cd1e655afeeb160f396d46 /src/entry.c
parent31632d4edbd903826cbf8f6bb93969f0d1a983fb (diff)
downloadevery-41f73b0a45ba3e63ac33ebdc99f12481f5b7cdab.tar.gz
implement printing
Diffstat (limited to 'src/entry.c')
-rw-r--r--src/entry.c219
1 files changed, 71 insertions, 148 deletions
diff --git a/src/entry.c b/src/entry.c
index f903d97..13208ca 100644
--- a/src/entry.c
+++ b/src/entry.c
@@ -2,177 +2,100 @@
#include <assert.h>
-static int sub_inc_year(struct tm *t, int n)
-{
- if (n == 0)
- return 0;
-
- t->tm_year += n;
-
- if (mktime(t) == -1)
- return -1;
-
- return 0;
-}
+static time_t rt;
-static int sub_inc_month(struct tm *t, int n)
+/* 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)
{
- unsigned i;
-
- if (n == 0)
- return 0;
-
- for (i = 0; i < n; i++) {
- t->tm_mon++;
- if (mktime(t) == -1) {
- t->tm_mon = 0;
- if (sub_inc_year(t, 1) == -1)
- return -1;
- }
- }
-
- return 0;
-}
+ struct tm t, ttmp, *tmp;
+ time_t rttmp, now_plus_warn, now_minus_stay;
+
+ assert(e != NULL);
-static int sub_inc_day(struct tm *t, int n)
-{
- unsigned i;
+ tmp = localtime(&now);
+ if (tmp == NULL)
+ return NULL;
+ t = *tmp;
+ ttmp = t;
- if (n == 0)
- return 0;
+ 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;
- for (i = 0; i < n; i++) {
- t->tm_mday++;
- if (mktime(t) == -1) {
- t->tm_mday = 1;
- if (sub_inc_month(t, 1) == -1)
- return -1;
- }
- }
+ now_plus_warn = mktime(&t);
- return 0;
-}
+ 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;
-static int sub_inc_hour(struct tm *t, int n)
-{
- unsigned i;
+ now_minus_stay = mktime(&ttmp);
- if (n == 0)
- return 0;
+ if (now_plus_warn == -1 || now_minus_stay == -1)
+ return NULL;
- for (i = 0; i < n; i++) {
- t->tm_hour++;
- if (mktime(t) == -1) {
- t->tm_hour = 0;
- if (sub_inc_day(t, 1) == -1)
- return -1;
+ if (e->type == ENTRY_TYPE_ON) {
+ if (e->start >= now_minus_stay && e->start <= now_plus_warn) {
+ rt = e->start;
+ return &rt;
}
- }
-
- return 0;
-}
-
-static int sub_inc_minute(struct tm *t, int n)
-{
- unsigned i;
- if (n == 0)
- return 0;
-
- for (i = 0; i < n; i++) {
- t->tm_min++;
- if (mktime(t) == -1) {
- t->tm_min = 0;
- if (sub_inc_hour(t, 1) == -1)
- return -1;
- }
+ return NULL;
}
- return 0;
-}
-
-static int sub_inc_second(struct tm *t, int n)
-{
- time_t rt;
- struct tm *tmp;
-
- if (n == 0)
- return 0;
-
- rt = mktime(t);
-
- if (rt + n <= rt)
- return -1;
- rt += n;
+ if (e->has_end && now_minus_stay > e->end)
+ return NULL;
- tmp = localtime(&rt);
- *t = *tmp;
-
- return 0;
-}
+ tmp = localtime( &(e->start) );
+ if (tmp == NULL)
+ return NULL;
+ t = *tmp;
-/* super inefficient, but should best-possible avoid any weird leap
- * years/seconds/dst-problems/other future changes */
-bool entry_is_active(struct entry_s *e, time_t now)
-{
- struct tm t, *tmp;
- time_t rt, now_plus_warn;
-
- assert(e != NULL);
+ for (rt = e->start;;) {
+ if (rt > now_plus_warn)
+ return NULL;
- tmp = localtime(&now);
- assert(tmp != NULL);
- t = *tmp;
+ if (rt >= now_minus_stay)
+ return &rt;
- if (sub_inc_year(&t, e->warn.year))
- return false;
- if (sub_inc_month(&t, e->warn.month))
- return false;
- if (sub_inc_day(&t, e->warn.day))
- return false;
- if (sub_inc_hour(&t, e->warn.hour))
- return false;
- if (sub_inc_minute(&t, e->warn.minute))
- return false;
- if (sub_inc_second(&t, e->warn.second))
- return false;
+ ttmp = t;
- now_plus_warn = mktime(&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;
- if (e->type == ENTRY_TYPE_ON) {
- if (e->start >= now && e->start <= now_plus_warn)
- return true;
+ rt = mktime(&t);
- return false;
- }
+ if (rt == -1) {
+ rttmp = mktime(&ttmp);
+ if (rttmp == -1)
+ return NULL;
- tmp = localtime(&(e->start));
- assert(tmp != NULL);
- t = *tmp;
+ tmp = localtime(&rttmp);
+ if (tmp == NULL)
+ return NULL;
+ t = *tmp;
- if (e->has_end && now > e->end)
- return false;
+ 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;
- for (rt = e->start;;) {
- rt = mktime(&t);
+ rt = mktime(&t);
- if (rt > now_plus_warn)
- return false;
-
- if (rt >= now)
- return true;
-
- if (sub_inc_year(&t, e->every.year))
- return false;
- if (sub_inc_month(&t, e->every.month))
- return false;
- if (sub_inc_day(&t, e->every.day))
- return false;
- if (sub_inc_hour(&t, e->every.hour))
- return false;
- if (sub_inc_minute(&t, e->every.minute))
- return false;
- if (sub_inc_second(&t, e->every.second))
- return false;
+ if (rt == -1)
+ return NULL;
+ }
}
}