Skip to content

Commit 448c352

Browse files
committed
Fixed date handling for Python 2.4.
1 parent 47364e2 commit 448c352

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

doc/api/core.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Core
88
of the :mod:`github2` package, but it is documented to aid contributors
99
to the package.
1010

11+
.. autofunction:: strptime
12+
1113
.. autofunction:: ghdate_to_datetime
1214
.. autofunction:: datetime_to_ghdate
1315

github2/core.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import time
2+
13
from datetime import datetime
24

35
GITHUB_TIMEZONE = "-0700"
@@ -6,13 +8,27 @@
68
COMMIT_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S"
79

810

11+
def strptime(string, format):
12+
"""Parse date strings according to specified format
13+
14+
We have to create our :obj:`~datetime.datetime` objects indirectly to remain
15+
compatible with Python 2.4, where the :class:`~datetime.datetime` class has
16+
no :meth:`~datetime.datetime.strptime` method.
17+
18+
:param str string: String to parse
19+
:param str format: Datetime format
20+
:return datetime: Parsed datetime
21+
"""
22+
return datetime(*(time.strptime(string, format)[:6]))
23+
24+
925
def ghdate_to_datetime(github_date):
1026
"""Convert Github date string to Python datetime
1127
1228
:param str github_date: date string to parse
1329
"""
1430
date_without_tz = " ".join(github_date.strip().split()[:2])
15-
return datetime.strptime(date_without_tz, GITHUB_DATE_FORMAT)
31+
return strptime(date_without_tz, GITHUB_DATE_FORMAT)
1632

1733

1834
def datetime_to_ghdate(datetime_):
@@ -30,7 +46,7 @@ def commitdate_to_datetime(commit_date):
3046
:param str github_date: date string to parse
3147
"""
3248
date_without_tz = commit_date[:-6]
33-
return datetime.strptime(date_without_tz, COMMIT_DATE_FORMAT)
49+
return strptime(date_without_tz, COMMIT_DATE_FORMAT)
3450

3551

3652
def datetime_to_commitdate(datetime_):

0 commit comments

Comments
 (0)