From 4a1991773b79c50d4828091f58d2e5b0077ade96 Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Mon, 31 Aug 2020 10:43:24 +0200 Subject: 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 +++++ 1 file changed, 5 insertions(+) (limited to 'git/objects/util.py') diff --git a/git/objects/util.py b/git/objects/util.py index b02479b7..d15d83c3 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: -- cgit v1.2.1