summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlasdair Colley <ac@brede.(none)>2011-11-02 15:27:07 +0000
committerAlasdair Colley <ac@brede.(none)>2011-11-02 15:27:07 +0000
commitf43802c3dd7d413494049e0bffd43f11b655b05b (patch)
tree1612860b2b5cdd259312e236b75f4668e3d5020b
parent1099e2eff77501874c11cf7b284069b71ff4239a (diff)
fixed bug with addEvent registering that event had been added when no such thing had happened
-rwxr-xr-xperc55
1 files changed, 26 insertions, 29 deletions
diff --git a/perc b/perc
index 23242be..37ed829 100755
--- a/perc
+++ b/perc
@@ -54,6 +54,9 @@ CONFIG_FILE = "perc.cfg"
NOW = datetime.now()
+ONE_MINUTE = timedelta(minutes=1)
+ONE_DAY = timedelta(days=1)
+
MODES = ["user", "admin"]
USER = None
@@ -169,7 +172,7 @@ def parseTime(timestr):
for key, value in CONFIG["FORMAT"]["time"].items():
try:
t = datetime.strptime(timestr, value)
- return NOW.replace(hour=t.hour, minute=t.minute, second=t.second, microsecond=t.microsecond), value
+ return (datetime.combine(NOW.date(), t.time()), value)
except ValueError:
pass
@@ -179,17 +182,8 @@ def parseTime(timestr):
def isImportantTemporalEvent(event):
return event in IMPORTANT_TEMPORAL_EVENTS + CONFIG["CUSTOM_EVENTS"].keys()
-def isTime(timestr):
- for key, value in CONFIG["STANDARDS"].items():
-
- m = re.search(value, timestr)
-
- if m: return True
-
- return False
-
def getDayOfWeek(day):
- return (NOW - day_of_week() * timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0) + day * timedelta(days=1)
+ return (NOW - day_of_week() * ONE_DAY).replace(hour=0, minute=0, second=0, microsecond=0) + day * ONE_DAY
def parseTemporalEvent(event, user):
start, finish = None, None
@@ -202,26 +196,28 @@ def parseTemporalEvent(event, user):
start_formatted = start.strftime(format).format(name=event)
finish_formatted = finish.strftime(format).format(name=event)
elif event == "LATER":
- start = NOW - timedelta(minutes=(random.randint(1, 400) + random.randint(1, 400)) / 2)
- finish = NOW + timedelta(minutes=(random.randint(1, 400) + random.randint(1, 400)) / 2)
+ start = NOW - ONE_MINUTE * random.randint(1, 400)
+ finish = NOW + ONE_MINUTE * random.randint(1, 400)
start_formatted = start.strftime("%H:%M")
finish_formatted = finish.strftime("%H:%M")
elif event == "tomorrow":
- start = NOW.replace(hour=0, minute=0, second=0, microsecond=0)
- finish = NOW.replace(day=NOW.day + 1, hour=0, minute=0, second=0, microsecond=0)
+ start = datetime.combine(NOW.date(), time())
+ finish = datetime.combine(NOW.date(), time()) + ONE_DAY
start_formatted = WEEK_DAY_NAMES[day_of_week()]
- finish_formatted = WEEK_DAY_NAMES[day_of_week(date.today() + timedelta(days=1))]
+ finish_formatted = WEEK_DAY_NAMES[day_of_week(date.today() + ONE_DAY)]
elif event in ["christmas", "xmas"]:
- start = NOW.replace(year=NOW.year - 1, month=12, day=25, hour=0, minute=0, second=0, microsecond=0)
- finish = NOW.replace(year=NOW.year, month=12, day=25, hour=0, minute=0, second=0, microsecond=0)
+ xmas = date(year=1900, month=12, day=25)
+ start = datetime.combine(xmas.replace(NOW.year - 1), time())
+ finish = datetime.combine(xmas.replace(NOW.year), time())
start_formatted = "%s %d" % (string.capwords(event), start.year)
finish_formatted = "%s %d" % (string.capwords(event), finish.year)
elif event in ["new year", "newyear"]:
- start = NOW.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
- finish = NOW.replace(year=NOW.year + 1, month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
+ newyear = date(year=1900, month=1, day=1)
+ start = datetime.combine(newyear.replace(year=NOW.year), time())
+ finish = datetime.combine(newyear.replace(year=NOW.year + 1), time())
start_formatted = str(start.year)
finish_formatted = str(finish.year)
@@ -243,14 +239,15 @@ def parseTemporalEvent(event, user):
start_formatted = "Monday"
finish_formatted = "Friday"
elif event in ["midday", "noon"]:
- start = NOW.replace(hour=0, minute=0, second=0, microsecond=0)
- finish = NOW.replace(hour=12, minute=0, second=0, microsecond=0)
+ start = datetime.combine(NOW.date(), time())
+ finish = datetime.combine(NOW.date(), time(hour=12))
start_formatted = "Midnight"
finish_formatted = string.capwords(event)
elif event == "halloween":
- start = NOW.replace(year=NOW.year - 1, month=10, day=31, hour=0, minute=0, second=0, microsecond=0)
- finish = NOW.replace(year=NOW.year, month=10, day=31, hour=0, minute=0, second=0, microsecond=0)
+ halloween = date(year=1900, month=10, day=31)
+ start = datetime.combine(halloween.replace(NOW.year - 1), time())
+ finish = datetime.combine(halloween.replace(NOW.year), time())
start_formatted = "Halloween %d" % start.year
finish_formatted = "Halloween %d" % finish.year
@@ -314,11 +311,6 @@ def addEvent(name, start, finish, format="{name} %Y"):
print "Event cannot have a start time later than the finish"
sys.exit(1)
- if name in CONFIG["CUSTOM_EVENTS"]:
- print "Replaced existing event with new info"
- else:
- print "Event added: '%s'" % name
-
event = {
"start": start.strftime(EVENT_DATETIME_FORMAT),
"finish": finish.strftime(EVENT_DATETIME_FORMAT),
@@ -327,6 +319,11 @@ def addEvent(name, start, finish, format="{name} %Y"):
CONFIG["CUSTOM_EVENTS"][name] = event
+ if name in CONFIG["CUSTOM_EVENTS"]:
+ print "Replaced existing event with new info"
+ else:
+ print "Event added: '%s'" % name
+
global CONFIG_IS_DIRTY
CONFIG_IS_DIRTY = True