Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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