From a2c78a13c39ddfe9b31500e5062ff720383d3412 Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Tue, 17 Oct 2023 21:40:31 +0900 Subject: [PATCH] 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)