Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygit2: convert libgit2/pygit2 timezone offsets to standard git offsets #279

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/scmrepo/git/backend/pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,15 @@ 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,
commit.committer.email,
commit.author.name,
commit.author.email,
commit.author.time,
commit.author.offset,
commit.author.offset * 60,
)

def _get_stash(self, ref: str):
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions src/scmrepo/git/objects.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import stat
from abc import ABC, abstractmethod
from dataclasses import dataclass
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)