fix time calculations for warnings
authorThomas Viehmann <tv@beamnet.de>
Tue, 30 Dec 2008 15:39:20 +0000 (16:39 +0100)
committerThomas Viehmann <tv@beamnet.de>
Tue, 30 Dec 2008 15:39:20 +0000 (16:39 +0100)
Signed-off-by: Thomas Viehmann <tv@beamnet.de>
Dak/plugin.py

index 2d99650..7c8ada2 100644 (file)
@@ -1,9 +1,11 @@
 ###
 # Copyright (c) 2008, Joerg Jaspert
+# Copyright (c) 2008, Thomas Viehmann
 # GPL v2 (not later)
 ###
 
 import time
+import datetime
 import config
 import re
 
@@ -29,21 +31,36 @@ class Dak(callbacks.Plugin):
         self.fname = "dinstallcheck"
         self.dinstallhour = [1, 7, 13, 19]
         self.dinstallmin = self.registryValue('dinstallminute')
-        warntime=10
-        self.warnframe = [i%60 for i in range(self.dinstallmin-warntime, self.dinstallmin+warntime+1, 1)]
-        self.dinstallframe = [i%60 for i in range(self.dinstallmin, self.dinstallmin+5, 1)]
+        self.warntime=10
+        self.dinstallduration=4
         self.channel = self.registryValue('channel')
         self.locks = {}
 
         def checktime():
-            (undef, undef, undef, hour, minute, undef, undef, undef, undef) = time.gmtime()
             log.debug("DAK: Regular dinstall time check")
-            if hour in self.dinstallhour:
-                log.debug("DAK: It is DINSTALL hour")
-                if minute in self.warnframe:
+
+            now = datetime.datetime.now()
+            # figure out time (in minutes) to next and from last dinstall
+            nextdinstall = None
+            lastdinstall = None
+            for h in self.dinstallhour:
+                ndt = now.replace(hour=h, minute=self.dinstallmin)
+                ldt = ndt
+                if ndt < now:
+                    ndt += datetime.timedelta(1)
+                if ldt > now:
+                    ldt -= datetime.timedelta(1)
+                ndt = int((ldt-now).seconds/60)
+                ldt = int((now-ldt).seconds/60)
+                if nextdinstall == None or ndt < nextdinstall:
+                    nextdinstall = ndt
+                if lastdinstall == None or ldt < lastdinstall:
+                    lastdinstall = ldt
+
+            if pdt <= self.warntime or ndt <= self.wantime:
                     log.debug("DAK: In Dinstall Warnframe")
                     msgMaker = ircmsgs.privmsg
-                    if minute in self.dinstallframe:
+                    if pdt <= self.dinstallduration:
                         log.debug("DAK: In Dinstall timeframe")
                         # No longer time to warn only, now is time to act, if we haven't already
                         if self.registryValue('dinstall') == True:
@@ -76,12 +93,6 @@ class Dak(callbacks.Plugin):
                                 irc.queueMsg(msgMaker(self.channel, "%s: DINSTALL soon, hurry up" % (self.locks[key]) ))
                                 if key == "ALL":
                                     irc.queueMsg(msgMaker(self.channel, "%s: ALL locked. If you want to keep that, remember turning off cron" % (self.locks[key]) ))
-                else: # if minute in self.warnframe
-                    # Outside our warnframe.
-                    log.debug("DAK: Not in warnframe")
-                    conf.supybot.plugins.Dak.get('dinstall').setValue(False)
-                    conf.supybot.plugins.Dak.get('warned').setValue(False)
-
             else: # if hour in self.dinstallhour
                 # We are far outside a dinstall start
                 log.debug("DAK: Not even dinstall hour")
@@ -175,8 +186,8 @@ class Dak(callbacks.Plugin):
         text.append("Dinstall hour: %s, minute: %s" % (self.dinstallhour, self.dinstallmin))
         if len(self.locks) > 1:
             text.append("Current locks: %d (%s)" % (len(self.locks), self.locks))
-        text.append("Warnframe: %s" % (self.warnframe))
-        text.append("Dinstallframe: %s" % (self.dinstallframe))
+        text.append("Warnframe: dinstall-%d to dinstall+%d" % (self.warntime,self.warntime))
+        text.append("Dinstallframe: dinstall to dinstall+%d" % (self.dinstallduration,))
         text.append("Dinstall flag: %s, Warnflag: %s" % (conf.supybot.plugins.Dak.get('dinstall'), conf.supybot.plugins.Dak.get('warned')))
         for key in text:
             irc.reply(key)