Skip to content

Instantly share code, notes, and snippets.

@ojii
Created September 27, 2010 11:44
Show Gist options
  • Save ojii/598900 to your computer and use it in GitHub Desktop.
Save ojii/598900 to your computer and use it in GitHub Desktop.
from datetime import date, timedelta
from django.db.models import Q
def next_birthdays(qs, days, fieldname):
def f(*args):
args = list(args)
last = args.pop(-1)
names = [fieldname] + args
return {'__'.join(names): last}
now = date.today()
limit = now + timedelta(days=days)
lo_day = now.day
lo_month = now.month
hi_day = limit.day
hi_month = limit.month
q = Q(f('month', 'gte', lo_month))
q &= Q(f('day', 'gte', lo_day))
q &= Q(f('month', 'lt', hi_month))
q &= Q(f('day', 'lt', hi_day))
return qs.filter(q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment