Skip to content

Commit 5925b3b

Browse files
CaedenPHgithub-actions
authored andcommitted
Fix get top billionaires BROKEN file (TheAlgorithms#8970)
* updating DIRECTORY.md * fix(get-top-billionaires): Handle timestamp before epoch * updating DIRECTORY.md * revert(pyproject): Re-implement ignore lru_cache * fix(age): Update age to current year * fix(doctest): Make years since dynamic --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent f4bb861 commit 5925b3b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,7 @@
12211221
* [Get Amazon Product Data](web_programming/get_amazon_product_data.py)
12221222
* [Get Imdb Top 250 Movies Csv](web_programming/get_imdb_top_250_movies_csv.py)
12231223
* [Get Imdbtop](web_programming/get_imdbtop.py)
1224+
* [Get Top Billionaires](web_programming/get_top_billionaires.py)
12241225
* [Get Top Hn Posts](web_programming/get_top_hn_posts.py)
12251226
* [Get User Tweets](web_programming/get_user_tweets.py)
12261227
* [Giphy](web_programming/giphy.py)

Diff for: web_programming/get_top_billionaires.py.disabled renamed to web_programming/get_top_billionaires.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This works for some of us but fails for others.
44
"""
55

6-
from datetime import datetime
6+
from datetime import UTC, datetime, timedelta
77

88
import requests
99
from rich import box
@@ -20,18 +20,31 @@
2020
)
2121

2222

23-
def calculate_age(unix_date: int) -> str:
23+
def calculate_age(unix_date: float) -> str:
2424
"""Calculates age from given unix time format.
2525
2626
Returns:
2727
Age as string
2828
29-
>>> calculate_age(-657244800000)
30-
'73'
31-
>>> calculate_age(46915200000)
32-
'51'
29+
>>> from datetime import datetime, UTC
30+
>>> years_since_create = datetime.now(tz=UTC).year - 2022
31+
>>> int(calculate_age(-657244800000)) - years_since_create
32+
73
33+
>>> int(calculate_age(46915200000)) - years_since_create
34+
51
3335
"""
34-
birthdate = datetime.fromtimestamp(unix_date / 1000).date()
36+
# Convert date from milliseconds to seconds
37+
unix_date /= 1000
38+
39+
if unix_date < 0:
40+
# Handle timestamp before epoch
41+
epoch = datetime.fromtimestamp(0, tz=UTC)
42+
seconds_since_epoch = (datetime.now(tz=UTC) - epoch).seconds
43+
birthdate = (
44+
epoch - timedelta(seconds=abs(unix_date) - seconds_since_epoch)
45+
).date()
46+
else:
47+
birthdate = datetime.fromtimestamp(unix_date, tz=UTC).date()
3548
return str(
3649
TODAY.year
3750
- birthdate.year

0 commit comments

Comments
 (0)