]> git.netwichtig.de Git - user/henk/code/snooze.git/blobdiff - snooze.c
Reschedule when time moved backwards
[user/henk/code/snooze.git] / snooze.c
index b88db77e1c3547aaad191bfcf9b1854c8aeb7f9e..3a5185d7f13a30fbd7354e379de0fe6960f4cb0d 100644 (file)
--- a/snooze.c
+++ b/snooze.c
@@ -31,7 +31,7 @@ static long slack = 60;
 #define SLEEP_PHASE 300
 static int nflag, vflag;
 
-static int timewait = 1;
+static int timewait = -1;
 static int randdelay = 0;
 static char *timefile;
 
@@ -64,6 +64,34 @@ parse_int(char **s, size_t minn, size_t maxn)
        return n;
 }
 
+static long
+parse_dur(char *s)
+{
+       long n;
+       char *end;
+
+       errno = 0;
+       n = strtol(s, &end, 10);
+       if (errno) {
+               perror("strtol");
+               exit(1);
+       }
+       if (n < 0) {
+               fprintf(stderr, "negative duration\n");
+               exit(1);
+       }
+       switch (*end) {
+       case 'm': n *= 60; break;
+       case 'h': n *= 60*60; break;
+       case 'd': n *= 24*60*60; break;
+       case 0: break;
+       default:
+               fprintf(stderr, "junk after duration: %s\n", end);
+               exit(1);
+       }
+       return n;
+}
+
 static int
 parse(char *expr, char *buf, long bufsiz, int offset)
 {
@@ -200,7 +228,9 @@ isotime(const struct tm *tm)
 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);
@@ -228,10 +258,10 @@ int main(int argc, char *argv[])
                        break;
                case 'n': nflag++; break;
                case 'v': vflag++; break;
-               case 's': slack = atoi(optarg); break;
-               case 'T': timewait = atoi(optarg); break;
+               case 's': slack = parse_dur(optarg); break;
+               case 'T': timewait = parse_dur(optarg); break;
                case 't': timefile = optarg; break;
-               case 'R': randdelay = atoi(optarg); break;
+               case 'R': randdelay = parse_dur(optarg); break;
                default:
                         fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
                            "  [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
@@ -248,8 +278,16 @@ int main(int argc, char *argv[])
                if (stat(timefile, &st) < 0) {
                        if (errno != ENOENT)
                                perror("stat");
+                       t = start - slack - 1 - timewait;
+               } else {
+                       t = st.st_mtime + 1;
+               }
+               if (timewait == -1) {
+                       while (t < start - slack)
+                               t = find_next(t + 1);
+                       start = t;
                } else {
-                       if (st.st_mtime + timewait > start)
+                       if (t + timewait > start)
                                start = st.st_mtime + timewait;
                }
        }
@@ -271,7 +309,7 @@ int main(int argc, char *argv[])
                start += delay;
        }
 
-       time_t t = find_next(start);
+       t = find_next(start);
        if (t < 0) {
                fprintf(stderr, "no satisfying date found within a year.\n");
                exit(2);
@@ -281,8 +319,18 @@ 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)
-                               printf("%s\n", isotime(localtime(&t)));
+                       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);
+                       }
                        t = find_next(t + 1);
                }
                exit(0);
@@ -301,6 +349,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
@@ -318,6 +371,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
                }