Skip to content

Instantly share code, notes, and snippets.

@jmieleiii
Created April 21, 2017 23:40
Show Gist options
  • Save jmieleiii/33cca560d9b92189af8c26082f18bf27 to your computer and use it in GitHub Desktop.
Save jmieleiii/33cca560d9b92189af8c26082f18bf27 to your computer and use it in GitHub Desktop.
daysOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def number_of_days_between_months(m1, m2):
days = 0
for month in range(m1, m2):
days += daysOfMonths[month]
return days
def days_alive(y1, m1, d1, y2, m2, d2):
days = 0
if y2 - y1 == 1:
if m2 > m1:
days += daysOfMonths[m1] - d1
days = days + number_of_days_between_months(m1, m2 - 1)
days = days + d2
if m1 >= 2:
days = days + 1
else:
days = d2 - d1
else:
for year in range(y1, y2 + 1):
# the first year may be an incomplete year
if y1 == year:
# calculate days left in month born
days += daysOfMonths[m1] - d1
# calculate days for each complete month for the rest of the year
days = days + number_of_days_between_months(m1, 11)
# account for a leap year
if is_leap_year(year) && m1 <= 2:
days = days + 1
# the last year is also incomplete
elif y1 == y2:
# calculate days lived in month, which is basically just adding d2
days = days + d2
# calculate days for each complete month for the year
days = days + number_of_days_between_months(0, m2 - 1)
# account for leap year
if is_leap_year(year) and m2 >= 2:
days = days + 1
# any years in between are completely lived
else:
for month in daysOfMonths:
days = days + daysOfMonths[month]
if is_leap_year(year):
days = days + 1
return days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment