]> git.netwichtig.de Git - user/henk/code/snooze.git/blobdiff - snooze.c
find_next: take leap year into account (e.g. when using dayofyear)
[user/henk/code/snooze.git] / snooze.c
index 60f7bffae8f813ee3e08929faeaca15a9201ee80..f37c42ac2a386453b9db812c59e97aa8d0dea0ed 100644 (file)
--- a/snooze.c
+++ b/snooze.c
@@ -1,16 +1,11 @@
 /*
  * snooze - run a command at a particular time
  *
- * To the extent possible under law,
- * Christian Neukirchen <chneukirchen@gmail.com>
+ * To the extent possible under law, Leah Neukirchen <leah@vuxu.org>
  * has waived all copyright and related or neighboring rights to this work.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
-/*
-##% gcc -Os -Wall -g -o $STEM $FILE -Wextra -Wwrite-strings
-*/
-
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -147,7 +142,7 @@ char weekday[8] = {0};
 char dayofmonth[31] = {0};
 char month[12] = {0};
 char dayofyear[366] = {0};
-char weekofyear[53] = {0};
+char weekofyear[54] = {0};
 char hour[24] = {0};
 char minute[60] = {0};
 char second[61] = {0};
@@ -190,7 +185,7 @@ next_day:
                tm->tm_hour = 0;
 
                t = mktime(tm);
-               if (t > from+(365*24*60*60))  // no result within a year
+               if (t > from+(366*24*60*60))  // no result within a year
                        return -1;
        }
 
@@ -230,6 +225,7 @@ int main(int argc, char *argv[])
        int c;
        time_t t;
        time_t now = time(0);
+       time_t last = 0;
 
        /* default: every day at 00:00:00 */
        memset(weekday, '*', sizeof weekday);
@@ -241,7 +237,7 @@ int main(int argc, char *argv[])
        minute[0] = '*';
        second[0] = '*';
 
-       while ((c = getopt(argc, argv, "D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
+       while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
                 switch(c) {
                case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
                case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
@@ -318,19 +314,22 @@ int main(int argc, char *argv[])
                /* dry-run, just output the next 5 dates. */
                int i;
                for (i = 0; i < 5; i++) {
-                       if (t > 0) {
-                               char weekstr[4];
-                               struct tm *tm = localtime(&t);
-                               strftime(weekstr, sizeof weekstr, "%a", tm);
-                               printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
-                                   isotime(tm),
-                                   weekstr,
-                                   ((t - now) / (60*60*24)),
-                                   ((t - now) / (60*60)) % 24,
-                                   ((t - now) / 60) % 60,
-                                   (t - now) % 60);
-                       }
+                       char weekstr[4];
+                       struct tm *tm = localtime(&t);
+                       strftime(weekstr, sizeof weekstr, "%a", tm);
+                       printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
+                           isotime(tm),
+                           weekstr,
+                           ((t - now) / (60*60*24)),
+                           ((t - now) / (60*60)) % 24,
+                           ((t - now) / 60) % 60,
+                           (t - now) % 60);
                        t = find_next(t + 1);
+                       if (t < 0) {
+                               fprintf(stderr,
+                                   "no satisfying date found within a year.\n");
+                               exit(2);
+                       }
                }
                exit(0);
        }
@@ -348,6 +347,11 @@ int main(int argc, char *argv[])
 
        while (!alarm_rang) {
                now = time(0);
+               if (now < last) {
+                       t = find_next(now);
+                       if (vflag)
+                               printf("Time moved backwards, rescheduled for %s\n", isotime(tm));
+               }
                t = mktime(tm);
                if (t <= now) {
                        if (now - t <= slack)  // still about time
@@ -365,6 +369,7 @@ int main(int argc, char *argv[])
                        struct timespec ts;
                        ts.tv_nsec = 0;
                        ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
+                       last = now;
                        nanosleep(&ts, 0);
                        // we just iterate again when this exits early
                }