diff --git a/osgithub/github.py b/osgithub/github.py index 3fec599..2a6161c 100644 --- a/osgithub/github.py +++ b/osgithub/github.py @@ -12,7 +12,7 @@ """ import json from base64 import b64decode -from datetime import datetime +from datetime import datetime, timezone from os import environ from pathlib import Path @@ -375,7 +375,11 @@ def get_last_updated(self, path, ref): """ commits = self.get_commits_for_file(path, ref, number_of_commits=1) last_commit_date = commits[0]["commit"]["committer"]["date"] - return datetime.strptime(last_commit_date, "%Y-%m-%dT%H:%M:%SZ") + dt = datetime.strptime(last_commit_date, "%Y-%m-%dT%H:%M:%SZ") + # we know GitHub is giving us a UTC timezone because the string ends in + # Z, but Python's strptime can't consume that with it's %Z operator so + # we're matching it literally and then setting the timezone to UTC. + return dt.replace(tzinfo=timezone.utc) def get_readme(self, tag="main"): """ diff --git a/tests/test_github.py b/tests/test_github.py index 3c6bdfd..e78996b 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -1,6 +1,6 @@ import json from base64 import b64encode -from datetime import datetime +from datetime import datetime, timezone from os import environ import pytest @@ -285,7 +285,7 @@ def test_github_repo_get_last_updated(httpretty): ) last_updated = repo.get_last_updated(path="test-folder/test-file.html", ref="main") - assert last_updated == datetime(2021, 3, 1, 10, 0, 0) + assert last_updated == datetime(2021, 3, 1, 10, 0, 0, tzinfo=timezone.utc) @pytest.mark.parametrize( @@ -546,7 +546,9 @@ def test_github_repo_get_contents_too_large_file(httpretty): content_file = repo.get_contents("test-folder/test-file.html", ref="main") assert content_file.decoded_content == str_content - assert content_file.last_updated == datetime(2021, 3, 1, 10, 0, 0) + assert content_file.last_updated == datetime( + 2021, 3, 1, 10, 0, 0, tzinfo=timezone.utc + ) def test_github_repo_get_readme(httpretty):