Skip to content

Commit 692d7e7

Browse files
authored
Merge pull request #372 from pizzapanther/master
Added Time Type
2 parents a8bb6ae + ad83a7e commit 692d7e7

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

docs/types/scalars.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ Graphene defines the following base Scalar Types:
99
- ``graphene.Boolean``
1010
- ``graphene.ID``
1111

12-
Graphene also provides custom scalars for Dates and JSON:
12+
Graphene also provides custom scalars for Dates, Times, and JSON:
1313

1414
- ``graphene.types.datetime.DateTime``
15+
- ``graphene.types.datetime.Time``
1516
- ``graphene.types.json.JSONString``
1617

1718

graphene/types/datetime.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,37 @@ def serialize(dt):
2929
)
3030
return dt.isoformat()
3131

32-
@staticmethod
33-
def parse_literal(node):
32+
@classmethod
33+
def parse_literal(cls, node):
3434
if isinstance(node, ast.StringValue):
35-
return iso8601.parse_date(node.value)
35+
return cls.parse_value(node.value)
3636

3737
@staticmethod
3838
def parse_value(value):
3939
return iso8601.parse_date(value)
40+
41+
42+
class Time(Scalar):
43+
'''
44+
The `Time` scalar type represents a Time value as
45+
specified by
46+
[iso8601](https://en.wikipedia.org/wiki/ISO_8601).
47+
'''
48+
epoch_date = '1970-01-01'
49+
50+
@staticmethod
51+
def serialize(time):
52+
assert isinstance(time, datetime.time), (
53+
'Received not compatible time "{}"'.format(repr(time))
54+
)
55+
return time.isoformat()
56+
57+
@classmethod
58+
def parse_literal(cls, node):
59+
if isinstance(node, ast.StringValue):
60+
return cls.parse_value(node.value)
61+
62+
@classmethod
63+
def parse_value(cls, value):
64+
dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value))
65+
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)

graphene/types/tests/test_datetime.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import datetime
22
import pytz
33

4-
from ..datetime import DateTime
4+
from ..datetime import DateTime, Time
55
from ..objecttype import ObjectType
66
from ..schema import Schema
77

88

99
class Query(ObjectType):
1010
datetime = DateTime(_in=DateTime(name='in'))
11+
time = Time(_at=Time(name='at'))
1112

1213
def resolve_datetime(self, args, context, info):
1314
_in = args.get('in')
1415
return _in
1516

17+
def resolve_time(self, args, context, info):
18+
return args.get('at')
19+
1620
schema = Schema(query=Query)
1721

1822

@@ -27,6 +31,17 @@ def test_datetime_query():
2731
}
2832

2933

34+
def test_time_query():
35+
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
36+
time = datetime.time(now.hour, now.minute, now.second, now.microsecond, now.tzinfo)
37+
isoformat = time.isoformat()
38+
39+
result = schema.execute('''{ time(at: "%s") }'''%isoformat)
40+
assert not result.errors
41+
assert result.data == {
42+
'time': isoformat
43+
}
44+
3045
def test_datetime_query_variable():
3146
now = datetime.datetime.now().replace(tzinfo=pytz.utc)
3247
isoformat = now.isoformat()

0 commit comments

Comments
 (0)