Skip to content

Commit

Permalink
git: add datetime properties for git objects
Browse files Browse the repository at this point in the history
  • Loading branch information
pmrowla committed Oct 17, 2023
1 parent c9ef6eb commit a2c78a1
Showing 1 changed file with 18 additions and 0 deletions.
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)

0 comments on commit a2c78a1

Please sign in to comment.