mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Fix get top billionaires BROKEN file (#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>
This commit is contained in:
parent
72c7b05caa
commit
5f7819e1cd
|
@ -1221,6 +1221,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)
|
||||
|
|
|
@ -3,7 +3,7 @@ CAUTION: You may get a json.decoding error.
|
|||
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
|
||||
|
@ -20,18 +20,31 @@ API_URL = (
|
|||
)
|
||||
|
||||
|
||||
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'
|
||||
>>> calculate_age(46915200000)
|
||||
'51'
|
||||
>>> from datetime import datetime, UTC
|
||||
>>> years_since_create = datetime.now(tz=UTC).year - 2022
|
||||
>>> int(calculate_age(-657244800000)) - years_since_create
|
||||
73
|
||||
>>> int(calculate_age(46915200000)) - years_since_create
|
||||
51
|
||||
"""
|
||||
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
|
Loading…
Reference in New Issue
Block a user