Skip to content

Commit dddb20a

Browse files
committed
added time type
1 parent 5cfa895 commit dddb20a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
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.epocj_time, value))
65+
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)

0 commit comments

Comments
 (0)