Skip to content

Commit

Permalink
Tighten up utils.get_local_time and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Dec 22, 2022
1 parent e83592b commit cfc1589
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
17 changes: 14 additions & 3 deletions newshomepages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,21 @@ def download_url(url: str, output_path: Path, timeout: int = 180):
f.write(chunk)


def get_local_time(data: typing.Dict) -> datetime:
"""Get the current time in the provided site's timezone."""
def get_local_time(site: typing.Dict) -> datetime:
"""Get the current time in the provided site's timezone.
Args:
site (dict): A site's data dictionary.
Returns the current item as a timezone-aware datetime object.
"""
# Get the current time
now = datetime.now()
tz = pytz.timezone(data["timezone"])

# Get the site's timezone
tz = pytz.timezone(site["timezone"])

# Cast the timestamp into the site's timezone and return it
return now.astimezone(tz)


Expand Down
15 changes: 9 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def test_write_json(tmpdir):
utils.write_json({"foo": "bar"}, p, verbose=False)


def test_get_local_time():
"""Test get_local_time."""
latimes = utils.get_site("latimes")
assert latimes["timezone"] == "America/Los_Angeles"
assert utils.get_local_time(latimes).tzinfo is not None
bbc = utils.get_site("bbc")
assert utils.get_local_time(bbc).tzname() == "GMT"


def test_sites():
"""Test sites utils."""
# Read in the list
Expand Down Expand Up @@ -99,9 +108,3 @@ def test_url_parse():
# utils.get_json_url(
# "https://archive.org/download/signalcleveland-2022/signalcleveland-2022-11-17T02%3A50%3A58.280867-05%3A00.wayback.json"
# )


def test_get_local_time():
"""Test method to get the local time."""
utils.get_local_time(utils.get_site("latimes"))
utils.get_local_time(utils.get_bundle("us-national"))

0 comments on commit cfc1589

Please sign in to comment.