Skip to content

Fix get top billionaires BROKEN file #8970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 18, 2023
Merged
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@
* [Get Amazon Product Data](web_programming/get_amazon_product_data.py)
* [Get Imdb Top 250 Movies Csv](web_programming/get_imdb_top_250_movies_csv.py)
* [Get Imdbtop](web_programming/get_imdbtop.py)
* [Get Top Billionaires](web_programming/get_top_billionaires.py)
* [Get Top Hn Posts](web_programming/get_top_hn_posts.py)
* [Get User Tweets](web_programming/get_user_tweets.py)
* [Giphy](web_programming/giphy.py)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This works for some of us but fails for others.
"""

from datetime import datetime
from datetime import UTC, datetime, timedelta

import requests
from rich import box
Expand All @@ -20,18 +20,29 @@
)


def calculate_age(unix_date: int) -> str:
def calculate_age(unix_date: float) -> str:
"""Calculates age from given unix time format.

Returns:
Age as string

>>> calculate_age(-657244800000)
'73'
'74'
>>> calculate_age(46915200000)
'51'
'52'
"""
birthdate = datetime.fromtimestamp(unix_date / 1000).date()
# Convert date from milliseconds to seconds
unix_date /= 1000

if unix_date < 0:
# Handle timestamp before epoch
epoch = datetime.fromtimestamp(0, tz=UTC)
seconds_since_epoch = (datetime.now(tz=UTC) - epoch).seconds
birthdate = (
epoch - timedelta(seconds=abs(unix_date) - seconds_since_epoch)
).date()
else:
birthdate = datetime.fromtimestamp(unix_date, tz=UTC).date()
return str(
TODAY.year
- birthdate.year
Expand Down