MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / years_old

Function years_old

web_programming/get_top_billionaires.py:30–58  ·  view source on GitHub ↗

Calculate the age in years based on the given birth date. Only the year, month, and day are used in the calculation. The time of day is ignored. Args: birth_timestamp: The date of birth. today: (useful for writing tests) or if None then datetime.date.today(). Ret

(birth_timestamp: int, today: date | None = None)

Source from the content-addressed store, hash-verified

28
29
30def years_old(birth_timestamp: int, today: date | None = None) -> int:
31 """
32 Calculate the age in years based on the given birth date. Only the year, month,
33 and day are used in the calculation. The time of day is ignored.
34
35 Args:
36 birth_timestamp: The date of birth.
37 today: (useful for writing tests) or if None then datetime.date.today().
38
39 Returns:
40 int: The age in years.
41
42 Examples:
43 >>> today = date(2024, 1, 12)
44 >>> years_old(birth_timestamp=datetime(1959, 11, 20).timestamp(), today=today)
45 64
46 >>> years_old(birth_timestamp=datetime(1970, 2, 13).timestamp(), today=today)
47 53
48 >>> all(
49 ... years_old(datetime(today.year - i, 1, 12).timestamp(), today=today) == i
50 ... for i in range(1, 111)
51 ... )
52 True
53 """
54 today = today or TODAY.date()
55 birth_date = datetime.fromtimestamp(birth_timestamp, tz=UTC).date()
56 return (today.year - birth_date.year) - (
57 (today.month, today.day) < (birth_date.month, birth_date.day)
58 )
59
60
61def get_forbes_real_time_billionaires() -> list[dict[str, int | str]]:

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected