Skip to content

Add support for getting "aware" datetime info #414

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

Merged
merged 2 commits into from
Apr 20, 2016
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
11 changes: 10 additions & 1 deletion git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
Serializable,
parse_date,
altz_to_utctz_str,
parse_actor_and_date
parse_actor_and_date,
from_timestamp,
)
from git.compat import text_type

Expand Down Expand Up @@ -144,6 +145,14 @@ def _set_cache_(self, attr):
super(Commit, self)._set_cache_(attr)
# END handle attrs

@property
def authored_datetime(self):
return from_timestamp(self.authored_date, self.author_tz_offset)

@property
def committed_datetime(self):
return from_timestamp(self.committed_date, self.committer_tz_offset)

@property
def summary(self):
""":return: First line of the commit message"""
Expand Down
30 changes: 29 additions & 1 deletion git/objects/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
from string import digits
import time
import calendar
from datetime import datetime, timedelta, tzinfo

__all__ = ('get_object_type_by_name', 'parse_date', 'parse_actor_and_date',
'ProcessStreamAdapter', 'Traversable', 'altz_to_utctz_str', 'utctz_to_altz',
'verify_utctz', 'Actor')
'verify_utctz', 'Actor', 'tzoffset', 'utc')

ZERO = timedelta(0)

#{ Functions

Expand Down Expand Up @@ -97,6 +100,31 @@ def verify_utctz(offset):
return offset


class tzoffset(tzinfo):
def __init__(self, secs_west_of_utc, name=None):
self._offset = timedelta(seconds=-secs_west_of_utc)
self._name = name or 'fixed'

def utcoffset(self, dt):
return self._offset

def tzname(self, dt):
return self._name

def dst(self, dt):
return ZERO


utc = tzoffset(0, 'UTC')


def from_timestamp(timestamp, tz_offset):
"""Converts a timestamp + tz_offset into an aware datetime instance."""
utc_dt = datetime.fromtimestamp(timestamp, utc)
local_dt = utc_dt.astimezone(tzoffset(tz_offset))
return local_dt


def parse_date(string_date):
"""
Parse the given date as one of the following
Expand Down
11 changes: 11 additions & 0 deletions git/test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import sys
import re
import os
from datetime import datetime
from git.objects.util import tzoffset, utc


def assert_commit_serialization(rwrepo, commit_id, print_performance_info=False):
Expand Down Expand Up @@ -343,3 +345,12 @@ def test_gpgsig(self):
cstream = BytesIO()
cmt._serialize(cstream)
assert not re.search(r"^gpgsig ", cstream.getvalue().decode('ascii'), re.MULTILINE)

def test_datetimes(self):
commit = self.rorepo.commit('4251bd5')
assert commit.authored_date == 1255018625
assert commit.committed_date == 1255026171
assert commit.authored_datetime == datetime(2009, 10, 8, 18, 17, 5, tzinfo=tzoffset(-7200)), commit.authored_datetime # noqa
assert commit.authored_datetime == datetime(2009, 10, 8, 16, 17, 5, tzinfo=utc), commit.authored_datetime
assert commit.committed_datetime == datetime(2009, 10, 8, 20, 22, 51, tzinfo=tzoffset(-7200))
assert commit.committed_datetime == datetime(2009, 10, 8, 18, 22, 51, tzinfo=utc), commit.committed_datetime