Skip to content

Commit

Permalink
Added get_age and age calculation methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanabani committed Oct 6, 2020
1 parent bbc62e2 commit 24dd34f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions sandpiper/user_info/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ def get_timezone(self, user_id: int) -> Optional[pytz.tzinfo.BaseTzInfo]:
@abstractmethod
def set_timezone(self, user_id: int, new_timezone: pytz.tzinfo.BaseTzInfo):
pass

@staticmethod
def _calculate_age(birthday: datetime.date, on_day: datetime.date):
birthday_this_year = datetime.date(on_day.year, birthday.month,
birthday.day)
age = on_day.year - birthday.year
if on_day < birthday_this_year:
return age - 1
return age

def get_age(self, user_id: int) -> Optional[int]:
birthday = self.get_birthday(user_id)
if birthday is None:
return None
return self._calculate_age(birthday, datetime.date.today())

This comment has been minimized.

Copy link
@Malivil

Malivil Oct 6, 2020

Contributor

Does this need to consider timezones?

This comment has been minimized.

Copy link
@Phanabani

Phanabani Oct 6, 2020

Author Owner

You're right, it does! I'll add an issue about this.

0 comments on commit 24dd34f

Please sign in to comment.