From 9281e982c3038fc359a03f5fc656976d431cc29c Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Mon, 31 Aug 2020 10:43:24 +0200 Subject: [PATCH 1/2] accept datetime instances as dates There's no easy way to re-create a commit (i.e. for rewriting purposes), because dates must be formatted as strings, passed, then parsed back. This patch allows parse_date() to accept datetime instances, such as those produced by from_timestamp() above. --- git/objects/util.py | 5 +++++ test/test_util.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/git/objects/util.py b/git/objects/util.py index b02479b7e..d15d83c35 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -135,6 +135,7 @@ def parse_date(string_date): """ Parse the given date as one of the following + * aware datetime instance * Git internal format: timestamp offset * RFC 2822: Thu, 07 Apr 2005 22:13:13 +0200. * ISO 8601 2005-04-07T22:13:13 @@ -144,6 +145,10 @@ def parse_date(string_date): :raise ValueError: If the format could not be understood :note: Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY. """ + if isinstance(string_date, datetime) and string_date.tzinfo: + offset = -int(string_date.utcoffset().total_seconds()) + return int(string_date.astimezone(utc).timestamp()), offset + # git time try: if string_date.count(' ') == 1 and string_date.rfind(':') == -1: diff --git a/test/test_util.py b/test/test_util.py index 26695df55..df4e54747 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -174,6 +174,10 @@ def test_user_id(self): self.assertIn('@', get_user_id()) def test_parse_date(self): + # parse_date(from_timestamp()) must return the tuple unchanged + for timestamp, offset in (1522827734, -7200), (1522827734, 0), (1522827734, +3600): + self.assertEqual(parse_date(from_timestamp(timestamp, offset)), (timestamp, offset)) + # test all supported formats def assert_rval(rval, veri_time, offset=0): self.assertEqual(len(rval), 2) @@ -200,6 +204,7 @@ def assert_rval(rval, veri_time, offset=0): # END for each date type # and failure + self.assertRaises(ValueError, parse_date, datetime.now()) # non-aware datetime self.assertRaises(ValueError, parse_date, 'invalid format') self.assertRaises(ValueError, parse_date, '123456789 -02000') self.assertRaises(ValueError, parse_date, ' 123456789 -0200') From 5e61cea9fc43b57e698ebc69382fb86905fd6388 Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Mon, 31 Aug 2020 11:26:03 +0200 Subject: [PATCH 2/2] add myself to authors --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 2047a5032..7b21b2b26 100644 --- a/AUTHORS +++ b/AUTHORS @@ -42,4 +42,5 @@ Contributors are: -Harmon -Liam Beguin -Ram Rachum +-Alba Mendez Portions derived from other open source works and are clearly marked.