From 2b2beb80943fd2a7521c537bc7696c60cc28220d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20M=C3=B6hn?= Date: Thu, 12 Feb 2015 09:08:24 +0100 Subject: [PATCH] Correct git-show recipe Make the diff generation more idiomatic and fix the assembling of the timestamp. git-show normally prints the author time, so use this instead of the commit time. Also fix how tzinfo is obtained. Of course we have to use the author's time zone and not some fixed one as I had written before. --- docs/recipes/git-show.rst | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/recipes/git-show.rst b/docs/recipes/git-show.rst index 249a6aa..0f4935d 100644 --- a/docs/recipes/git-show.rst +++ b/docs/recipes/git-show.rst @@ -31,7 +31,7 @@ Show SHA hash Show diff ====================================================================== - >>> diff = commit.parents[0].tree.diff_to_tree(commit.tree) + >>> diff = repo.diff(commit.parents[0], commit) ====================================================================== Show all files in commit @@ -41,14 +41,37 @@ Show all files in commit >>> print(e.name) ====================================================================== -Produce something like a `git show` message +Produce something like a ``git show`` message ====================================================================== +In order to display timezone information you have to create a subclass of +tzinfo as described in the `Python datetime documentation`_:: + + from datetime import tzinfo, timedelta + class FixedOffset(tzinfo): + """Fixed offset in minutes east from UTC.""" + + def __init__(self, offset): + self.__offset = timedelta(minutes = offset) + + def utcoffset(self, dt): + return self.__offset + + def tzname(self, dt): + return None + + def dst(self, dt): + return timedelta(0) # dealing with DST would be too complicated + +.. _Python datetime documentation: https://docs.python.org/2/library/datetime.html#tzinfo-objects + +Then you can make your message: + >>> from __future__ import unicode_literals >>> from datetime import datetime - >>> import pytz - >>> tzinfo = pytz.timezone('Europe/Berlin') - >>> dt = datetime.fromtimestamp(float(commit.commit_time), tzinfo) + >>> + >>> tzinfo = FixedOffset(commit.author.offset) + >>> dt = datetime.fromtimestamp(float(commit.author.time), tzinfo) >>> timestr = dt.strftime('%c %z') >>> msg = '\n'.join(['commit {}'.format(commit.tree_id.hex), ... 'Author: {} <{}>'.format(commit.author.name, commit.author.email),