Skip to content

Commit bf35a12

Browse files
committed
Added naive datetime implementation.
1 parent d5d2e0e commit bf35a12

2 files changed

Lines changed: 18 additions & 35 deletions

File tree

doc/api/core.rst

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

11-
.. autofunction:: strptime
12-
1311
.. autofunction:: ghdate_to_datetime
1412
.. autofunction:: datetime_to_ghdate
1513

github2/core.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,56 @@
11
import sys
2-
import time
32

43
from datetime import datetime
4+
from dateutil import (parser, tz)
5+
56

67
#: Running under Python 3
78
PY3K = sys.version_info[0] == 3 and True or False
89

9-
10-
GITHUB_TIMEZONE = "-0700"
11-
GITHUB_DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
12-
#2009-03-21T18:01:48-07:00
10+
GITHUB_DATE_FORMAT = "%Y/%m/%d %H:%M:%S %z"
11+
# We need to manually mangle the timezone for commit date formatting because it
12+
# uses -xx:xx format
1313
COMMIT_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S"
14-
COMMIT_TIMEZONE = "-07:00"
15-
16-
17-
def strptime(string, format):
18-
"""Parse date strings according to specified format
19-
20-
We have to create our :obj:`~datetime.datetime` objects indirectly to remain
21-
compatible with Python 2.4, where the :class:`~datetime.datetime` class has
22-
no :meth:`~datetime.datetime.strptime` method.
23-
24-
:param str string: String to parse
25-
:param str format: Datetime format
26-
:return datetime: Parsed datetime
27-
"""
28-
return datetime(*(time.strptime(string, format)[:6]))
14+
GITHUB_TZ = tz.gettz("America/Los_Angeles")
2915

3016

3117
def ghdate_to_datetime(github_date):
3218
"""Convert Github date string to Python datetime
3319
3420
:param str github_date: date string to parse
3521
"""
36-
date_without_tz = " ".join(github_date.strip().split()[:2])
37-
return strptime(date_without_tz, GITHUB_DATE_FORMAT)
22+
return parser.parse(github_date).replace(tzinfo=None)
3823

3924

4025
def datetime_to_ghdate(datetime_):
4126
"""Convert Python datetime to Github date string
4227
4328
:param datetime datetime_: datetime object to convert
4429
"""
45-
date_without_tz = datetime_.strftime(GITHUB_DATE_FORMAT)
46-
return " ".join([date_without_tz, GITHUB_TIMEZONE])
30+
datetime_ = datetime_.replace(tzinfo=GITHUB_TZ)
31+
return datetime_.strftime(GITHUB_DATE_FORMAT)
4732

4833

4934
def commitdate_to_datetime(commit_date):
5035
"""Convert commit date string to Python datetime
5136
5237
:param str github_date: date string to parse
5338
"""
54-
date_without_tz = commit_date[:-6]
55-
return strptime(date_without_tz, COMMIT_DATE_FORMAT)
39+
return parser.parse(commit_date).replace(tzinfo=None)
5640

5741

5842
def datetime_to_commitdate(datetime_):
5943
"""Convert Python datetime to Github date string
6044
6145
:param datetime datetime_: datetime object to convert
6246
"""
47+
datetime_ = datetime_.replace(tzinfo=GITHUB_TZ)
6348
date_without_tz = datetime_.strftime(COMMIT_DATE_FORMAT)
64-
return "".join([date_without_tz, COMMIT_TIMEZONE])
49+
utcoffset = GITHUB_TZ.utcoffset(datetime_)
50+
hours, minutes = divmod(utcoffset.days * 86400 + utcoffset.seconds, 3600)
51+
52+
return "".join([date_without_tz, "%+03d:%02d" % (hours, minutes)])
53+
6554

6655

6756
def userdate_to_datetime(user_date):
@@ -73,19 +62,15 @@ def userdate_to_datetime(user_date):
7362
7463
:param str user_date: date string to parse
7564
"""
76-
try:
77-
return ghdate_to_datetime(user_date)
78-
except ValueError:
79-
return strptime(user_date, '%Y-%m-%dT%H:%M:%SZ')
65+
return parser.parse(user_date).replace(tzinfo=None)
8066

8167

8268
def isodate_to_datetime(iso_date):
8369
"""Convert commit date string to Python datetime
8470
8571
:param str github_date: date string to parse
8672
"""
87-
date_without_tz = iso_date[:-1]
88-
return strptime(date_without_tz, COMMIT_DATE_FORMAT)
73+
return parser.parse(iso_date).replace(tzinfo=None)
8974

9075

9176
def datetime_to_isodate(datetime_):

0 commit comments

Comments
 (0)