0

We're coding a system that parses time based events, and we'd like to implement a suppression system for these events where we can give cron-like syntax for matching against.

e.g. suppress if...

  1. .. the event occurs on the last Friday of the month
  2. .. the event occurs between 4pm and 5pm weekdays
  3. .. the event occurs on a Sunday

and so on.

We're using Python so I was hoping there may be a module for doing this type of thing, but my search skills are letting me down (lots of results about scheduling events, whereas I want the opposite - does the current time match a defined schedule).

Alternatively I'm happy writing one from scratch if anyone has any suggestions for the best way of storing and comparing a time against a schedule.

Edit:

To clarify, the suppression schedules are not set in stone so I cannot simply code a function to handle specific cases. The schedules need to be stored in a database, and tied to an event, so I can do roughly:

sql_rows = ..... "SELECT * FROM suppressions WHERE event_id = ?"

for row in sql_rows:
    if matchesSchedule(current_time,row):
        print "This should be suppressed"

So I need to represent the schedule in a format that I can store in a table, and then match columns against the current time components.

AndyC
  • 2,513
  • 3
  • 17
  • 17
  • I Hope this may help http://docs.python.org/2/library/sched.html – Nikhil Rupanawar Aug 02 '13 at 08:14
  • @NikhilRupanawar Unfortunately no. As I mentioned I'm not scheduling events, more like testing to see if time matches a given schedule. – AndyC Aug 02 '13 at 08:29
  • If I understand you correctly, you just want to compare a given time to a set of time patterns and check whether it is matching one or more of these. The straight forward approach would be to convert your given time into a `datetime` object, then check that object for your contraints as, for instance, `d.weekday() in (4, 5, 6) and d.hour == 10`. Why isn't that possible? – Alfe Aug 02 '13 at 08:45

1 Answers1

0

I propose to simply implement your time pattern checkers yourself. That's pretty straight-forward using sth like the datetime object (you could use time as well if you like that better). And in Python syntax you can even express things like "the sum of the number of minutes, hours, and seconds shall be 42", i. e. things which are not possible using cron-like patterns.

import datetime

def matchesTimePattern1(d):
  return (
    d.weekday() == 4 and  # Friday?
    d.month != (d + datetime.timedelta(7)).month  # next week is different month?
  )

def matchesTimePattern2(d):
  return (
    d.weekday() in (0, 1, 2, 3, 4) and  # weekday?
    d.hour == 16  # at 4pm (up to 4:59:59pm)
  )

def matchesTimePattern3(d):
  return (
    d.weekday() == 6  # Sunday?
  )

d = datetime.datetime.now()
print matchesTimePattern1(d)
print matchesTimePattern2(d)
print matchesTimePattern3(d)
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • Thanks, it's more or less what I had in mind - my problem, and sorry I should have been clearer, is that I need one function to test the match against a defined list of schedules, e.g. schedules stored in a database. I'll edit my original question to reflect. – AndyC Aug 02 '13 at 09:20
  • 1
    So you need ① a language to express your scheduling rules (e. g. sth similar to the cron tab language), ② a parser for that language (i. e. a Python library, translating it into Python expressions like the one in my answer), and ③ a facility which tests given times against these computed rules. That's more like a programming task and not just a question fit for SO, I fear ;-) – Alfe Aug 02 '13 at 11:13
  • At first I didn't think it would be so complex, but as you piece it all together it does seem quite a challenge. Then again I also figured this type of thing would be quite common (I see a lot of software in my line of work that lets you set schedules for ignoring/suppressing events) so I hoped there was a library for comparing time against a schedule (in any language, since I could port it to Python). Guess it's not too common! – AndyC Aug 02 '13 at 11:25
  • And you might be correct. I'm just not fit to answer your call for such a library :-} – Alfe Aug 02 '13 at 11:28