Created
April 20, 2015 15:54
-
-
Save kostyll/bc74ac1c962c964e2b2b to your computer and use it in GitHub Desktop.
test gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
def yearsago(years, from_date=None): | |
if from_date is None: | |
from_date = datetime.now() | |
try: | |
return from_date.replace(year=from_date.year - years) | |
except: | |
# Must be 2/29! | |
assert from_date.month == 2 and from_date.day == 29 # can be removed | |
return from_date.replace(month=2, day=28, | |
year=from_date.year-years) | |
def num_years(begin, end=None): | |
if end is None: | |
end = datetime.now() | |
num_years = int((end - begin).days / 365.25) | |
if begin > yearsago(num_years, end): | |
return num_years - 1 | |
else: | |
return num_years |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment