Skip to content

Instantly share code, notes, and snippets.

@ojii
Created September 27, 2010 12:29
Show Gist options
  • Save ojii/598944 to your computer and use it in GitHub Desktop.
Save ojii/598944 to your computer and use it in GitHub Desktop.
def next_birthdays(qs, days, fieldname):
def s(part, op, val):
return "EXTRACT(%s FROM %s) %s %s" % (part, fieldname, op, val)
now = date.today()
limit = now + timedelta(days=days)
lo_day = now.day
lo_month = now.month
hi_day = limit.day
hi_month = limit.month
if hi_day > lo_day:
sql = [
s('day', '>=', lo_day),
s('month', '>=', lo_month),
s('day', '<', hi_day),
s('month', '<', hi_month),
]
else:
sql1 = [
s('day', '>=', lo_day),
s('month', '>=', lo_month),
s('month', '<', hi_month),
]
sql2 = [
s('month', '>=', lo_month),
s('day', '<', hi_day),
s('month', '<', hi_month),
]
sql = ['((%s) OR (%s))' % (' AND '.join(sql1), ' AND '.join(sql2))]
return qs.extra(where=sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment