Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4a19917

Browse filesBrowse files
mildsunriseByron
authored andcommitted
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.
1 parent 6ef3775 commit 4a19917
Copy full SHA for 4a19917

2 files changed

+10Lines changed: 10 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎git/objects/util.py‎

Copy file name to clipboardExpand all lines: git/objects/util.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def parse_date(string_date):
135135
"""
136136
Parse the given date as one of the following
137137
138+
* aware datetime instance
138139
* Git internal format: timestamp offset
139140
* RFC 2822: Thu, 07 Apr 2005 22:13:13 +0200.
140141
* ISO 8601 2005-04-07T22:13:13
@@ -144,6 +145,10 @@ def parse_date(string_date):
144145
:raise ValueError: If the format could not be understood
145146
:note: Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY.
146147
"""
148+
if isinstance(string_date, datetime) and string_date.tzinfo:
149+
offset = -int(string_date.utcoffset().total_seconds())
150+
return int(string_date.astimezone(utc).timestamp()), offset
151+
147152
# git time
148153
try:
149154
if string_date.count(' ') == 1 and string_date.rfind(':') == -1:
Collapse file

‎test/test_util.py‎

Copy file name to clipboardExpand all lines: test/test_util.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ def test_user_id(self):
174174
self.assertIn('@', get_user_id())
175175

176176
def test_parse_date(self):
177+
# parse_date(from_timestamp()) must return the tuple unchanged
178+
for timestamp, offset in (1522827734, -7200), (1522827734, 0), (1522827734, +3600):
179+
self.assertEqual(parse_date(from_timestamp(timestamp, offset)), (timestamp, offset))
180+
177181
# test all supported formats
178182
def assert_rval(rval, veri_time, offset=0):
179183
self.assertEqual(len(rval), 2)
@@ -200,6 +204,7 @@ def assert_rval(rval, veri_time, offset=0):
200204
# END for each date type
201205

202206
# and failure
207+
self.assertRaises(ValueError, parse_date, datetime.now()) # non-aware datetime
203208
self.assertRaises(ValueError, parse_date, 'invalid format')
204209
self.assertRaises(ValueError, parse_date, '123456789 -02000')
205210
self.assertRaises(ValueError, parse_date, ' 123456789 -0200')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.