Skip to content

Commit d04aeaa

Browse files
committed
Merge pull request #414 from nvie/support-full-datetimes-on-commits
Add support for getting "aware" datetime info
2 parents fcb6e88 + f77f977 commit d04aeaa

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

‎git/objects/commit.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
Serializable,
2222
parse_date,
2323
altz_to_utctz_str,
24-
parse_actor_and_date
24+
parse_actor_and_date,
25+
from_timestamp,
2526
)
2627
fromgit.compatimporttext_type
2728

@@ -144,6 +145,14 @@ def _set_cache_(self, attr):
144145
super(Commit, self)._set_cache_(attr)
145146
# END handle attrs
146147

148+
@property
149+
defauthored_datetime(self):
150+
returnfrom_timestamp(self.authored_date, self.author_tz_offset)
151+
152+
@property
153+
defcommitted_datetime(self):
154+
returnfrom_timestamp(self.committed_date, self.committer_tz_offset)
155+
147156
@property
148157
defsummary(self):
149158
""":return: First line of the commit message"""

‎git/objects/util.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
fromstringimportdigits
1616
importtime
1717
importcalendar
18+
fromdatetimeimportdatetime, timedelta, tzinfo
1819

1920
__all__= ('get_object_type_by_name', 'parse_date', 'parse_actor_and_date',
2021
'ProcessStreamAdapter', 'Traversable', 'altz_to_utctz_str', 'utctz_to_altz',
21-
'verify_utctz', 'Actor')
22+
'verify_utctz', 'Actor', 'tzoffset', 'utc')
23+
24+
ZERO=timedelta(0)
2225

2326
#{Functions
2427

@@ -97,6 +100,31 @@ def verify_utctz(offset):
97100
returnoffset
98101

99102

103+
classtzoffset(tzinfo):
104+
def__init__(self, secs_west_of_utc, name=None):
105+
self._offset=timedelta(seconds=-secs_west_of_utc)
106+
self._name=nameor'fixed'
107+
108+
defutcoffset(self, dt):
109+
returnself._offset
110+
111+
deftzname(self, dt):
112+
returnself._name
113+
114+
defdst(self, dt):
115+
returnZERO
116+
117+
118+
utc=tzoffset(0, 'UTC')
119+
120+
121+
deffrom_timestamp(timestamp, tz_offset):
122+
"""Converts a timestamp + tz_offset into an aware datetime instance."""
123+
utc_dt=datetime.fromtimestamp(timestamp, utc)
124+
local_dt=utc_dt.astimezone(tzoffset(tz_offset))
125+
returnlocal_dt
126+
127+
100128
defparse_date(string_date):
101129
"""
102130
Parse the given date as one of the following

‎git/test/test_commit.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
importsys
3333
importre
3434
importos
35+
fromdatetimeimportdatetime
36+
fromgit.objects.utilimporttzoffset, utc
3537

3638

3739
defassert_commit_serialization(rwrepo, commit_id, print_performance_info=False):
@@ -343,3 +345,12 @@ def test_gpgsig(self):
343345
cstream=BytesIO()
344346
cmt._serialize(cstream)
345347
assertnotre.search(r"^gpgsig ", cstream.getvalue().decode('ascii'), re.MULTILINE)
348+
349+
deftest_datetimes(self):
350+
commit=self.rorepo.commit('4251bd5')
351+
assertcommit.authored_date==1255018625
352+
assertcommit.committed_date==1255026171
353+
assertcommit.authored_datetime==datetime(2009, 10, 8, 18, 17, 5, tzinfo=tzoffset(-7200)), commit.authored_datetime# noqa
354+
assertcommit.authored_datetime==datetime(2009, 10, 8, 16, 17, 5, tzinfo=utc), commit.authored_datetime
355+
assertcommit.committed_datetime==datetime(2009, 10, 8, 20, 22, 51, tzinfo=tzoffset(-7200))
356+
assertcommit.committed_datetime==datetime(2009, 10, 8, 18, 22, 51, tzinfo=utc), commit.committed_datetime

0 commit comments

Comments
(0)