Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
leap_shift solution in Clear category for Next Birthday by flpo
from datetime import date, timedelta
from calendar import isleap
def next_birthday(today, birthdates):
def leap_shift(y, m, d):
if m == 2 and d == 29 and not isleap(y):
return date(y, 3, 1)
return date(y, m, d)
min_dist, today = 366, date(*today)
for name, (y, m, d) in birthdates.items():
birthday = leap_shift(today.year, m, d)
if birthday < today:
birthday = leap_shift(today.year + 1, m, d)
dist = (birthday - today).days
if dist < min_dist:
next_birthdays, min_dist = {}, dist
if dist == min_dist:
next_birthdays[name] = birthday.year - y
return min_dist, next_birthdays
Oct. 6, 2020