Skip to content

Commit a2c78a1

Browse files
committed
git: add datetime properties for git objects
1 parent c9ef6eb commit a2c78a1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/scmrepo/git/objects.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import stat
23
from abc import ABC, abstractmethod
34
from dataclasses import dataclass
@@ -12,6 +13,11 @@ def S_ISGITLINK(m: int) -> bool:
1213
return stat.S_IFMT(m) == S_IFGITLINK
1314

1415

16+
def _to_datetime(time: int, offset: int) -> datetime.datetime:
17+
tz = datetime.timezone(datetime.timedelta(seconds=offset))
18+
return datetime.datetime.fromtimestamp(time, tz=tz)
19+
20+
1521
class GitObject(ABC):
1622
@abstractmethod
1723
def open(self, mode: str = "r", encoding: str = None):
@@ -169,6 +175,14 @@ class GitCommit:
169175
author_time: int
170176
author_time_offset: int
171177

178+
@property
179+
def commit_datetime(self) -> datetime.datetime:
180+
return _to_datetime(self.commit_time, self.commit_time_offset)
181+
182+
@property
183+
def author_datetime(self) -> datetime.datetime:
184+
return _to_datetime(self.author_time, self.author_time_offset)
185+
172186

173187
@dataclass
174188
class GitTag:
@@ -180,3 +194,7 @@ class GitTag:
180194
tag_time: int
181195
tag_time_offset: int
182196
message: str
197+
198+
@property
199+
def tag_datetime(self) -> datetime.datetime:
200+
return _to_datetime(self.tag_time, self.tag_time_offset)

0 commit comments

Comments
 (0)