From ec4e388068f85ade03920b44c4f56f5bdaa65bc8 Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Tue, 17 Oct 2023 21:29:43 +0900 Subject: [PATCH 1/2] pygit2: return tz_offset in seconds from UTC --- src/scmrepo/git/backend/pygit2/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scmrepo/git/backend/pygit2/__init__.py b/src/scmrepo/git/backend/pygit2/__init__.py index e36ce090..da3a1e06 100644 --- a/src/scmrepo/git/backend/pygit2/__init__.py +++ b/src/scmrepo/git/backend/pygit2/__init__.py @@ -401,7 +401,7 @@ def resolve_commit(self, rev: str) -> "GitCommit": return GitCommit( str(commit.id), commit.commit_time, - commit.commit_time_offset, + commit.commit_time_offset * 60, commit.message, [str(parent) for parent in commit.parent_ids], commit.committer.name, @@ -409,7 +409,7 @@ def resolve_commit(self, rev: str) -> "GitCommit": commit.author.name, commit.author.email, commit.author.time, - commit.author.offset, + commit.author.offset * 60, ) def _get_stash(self, ref: str): @@ -962,7 +962,7 @@ def get_tag(self, name: str) -> Optional[Union[str, "GitTag"]]: tag.tagger.name, tag.tagger.email, tag.tagger.time, - tag.tagger.offset, + tag.tagger.offset * 60, tag.message, ) except KeyError: From 07f6484bb742b8b3506de81e97c27cb42be85fad Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Tue, 17 Oct 2023 21:40:31 +0900 Subject: [PATCH 2/2] git: add datetime properties for git objects --- src/scmrepo/git/objects.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/scmrepo/git/objects.py b/src/scmrepo/git/objects.py index 25a0afc8..192fd802 100644 --- a/src/scmrepo/git/objects.py +++ b/src/scmrepo/git/objects.py @@ -1,3 +1,4 @@ +import datetime import stat from abc import ABC, abstractmethod from dataclasses import dataclass @@ -12,6 +13,11 @@ def S_ISGITLINK(m: int) -> bool: return stat.S_IFMT(m) == S_IFGITLINK +def _to_datetime(time: int, offset: int) -> datetime.datetime: + tz = datetime.timezone(datetime.timedelta(seconds=offset)) + return datetime.datetime.fromtimestamp(time, tz=tz) + + class GitObject(ABC): @abstractmethod def open(self, mode: str = "r", encoding: str = None): @@ -169,6 +175,14 @@ class GitCommit: author_time: int author_time_offset: int + @property + def commit_datetime(self) -> datetime.datetime: + return _to_datetime(self.commit_time, self.commit_time_offset) + + @property + def author_datetime(self) -> datetime.datetime: + return _to_datetime(self.author_time, self.author_time_offset) + @dataclass class GitTag: @@ -180,3 +194,7 @@ class GitTag: tag_time: int tag_time_offset: int message: str + + @property + def tag_datetime(self) -> datetime.datetime: + return _to_datetime(self.tag_time, self.tag_time_offset)