Skip to content

Commit 99074a9

Browse files
committed
Handle User.created_at attributes as datetime objects.
1 parent 0173a49 commit 99074a9

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

doc/api/core.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Core
1616
.. autofunction:: commitdate_to_datetime
1717
.. autofunction:: datetime_to_commitdate
1818

19+
.. autofunction:: userdate_to_datetime
20+
1921
.. autofunction:: doc_generator
2022

2123
.. autoclass:: GithubCommand

github2/core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ def datetime_to_commitdate(datetime_):
5959
return "".join([date_without_tz, COMMIT_TIMEZONE])
6060

6161

62+
def userdate_to_datetime(user_date):
63+
"""Convert user date string to Python datetime
64+
65+
Unfortunately this needs a special case because :meth:`~Github.users.show`
66+
and :meth:`~Github.users.search` return a different formats for the
67+
`created_at` attributes.
68+
69+
:param str user_date: date string to parse
70+
"""
71+
try:
72+
return ghdate_to_datetime(user_date)
73+
except ValueError:
74+
return strptime(user_date, '%Y-%m-%dT%H:%M:%SZ')
75+
76+
6277
class GithubCommand(object):
6378

6479
def __init__(self, request):
@@ -143,6 +158,10 @@ class DateAttribute(Attribute):
143158
"to": commitdate_to_datetime,
144159
"from": datetime_to_commitdate,
145160
},
161+
"user": {
162+
"to" : userdate_to_datetime,
163+
"from": datetime_to_ghdate,
164+
}
146165
}
147166

148167
def __init__(self, *args, **kwargs):

github2/users.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from github2.core import BaseData, GithubCommand, Attribute
1+
from github2.core import BaseData, GithubCommand, DateAttribute, Attribute
22
import urllib
33

44

@@ -23,6 +23,8 @@ class User(BaseData):
2323
private_gist_count = Attribute(
2424
"Number of private gists owned by the user")
2525
plan = Attribute("Current active github plan")
26+
created_at = DateAttribute("The date this user was registered",
27+
format="user")
2628

2729
def is_authenticated(self):
2830
"""Test for user auththenication

tests/test_user.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import _setup
22

3+
import datetime
4+
35
from nose.tools import (assert_equals, assert_false, assert_true)
46

57
import utils
@@ -18,10 +20,8 @@ def test_user(self):
1820

1921
def test_meta(self):
2022
user = self.client.users.show('defunkt')
21-
# Difficult to handle created_at attribute as its content varies
22-
# depending on API path.
23-
#assert_equals(user.created_at,
24-
# datetime.datetime(2007, 10, 19, 22, 24, 19))
23+
assert_equals(user.created_at,
24+
datetime.datetime(2007, 10, 19, 22, 24, 19))
2525
assert_equals(user.followers_count, 2593)
2626
assert_equals(user.following_count, 212)
2727
assert_equals(user.gravatar_id, 'b8dbb1987e8e5318584865f880036796')

0 commit comments

Comments
 (0)