mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 11:33:55 +03:00
Added datetime scalar
This commit is contained in:
parent
6e0789bfcd
commit
ec4a49498d
39
graphene/new_types/datetime.py
Normal file
39
graphene/new_types/datetime.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
import datetime
|
||||
|
||||
from graphql.language import ast
|
||||
|
||||
from .scalars import Scalar
|
||||
|
||||
try:
|
||||
import iso8601
|
||||
except:
|
||||
raise ImportError(
|
||||
"iso8601 package is required for DateTime Scalar.\n"
|
||||
"You can install it using: pip install iso8601."
|
||||
)
|
||||
|
||||
|
||||
class DateTime(Scalar):
|
||||
'''
|
||||
The `DateTime` scalar type represents a DateTime
|
||||
value as specified by
|
||||
[iso8601](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
'''
|
||||
|
||||
@staticmethod
|
||||
def serialize(dt):
|
||||
assert isinstance(dt, (datetime.datetime, datetime.date)), (
|
||||
'Received not compatible datetime "{}"'.format(repr(dt))
|
||||
)
|
||||
return dt.isoformat()
|
||||
|
||||
@staticmethod
|
||||
def parse_literal(node):
|
||||
if isinstance(node, ast.StringValue):
|
||||
return iso8601.parse_date(node.value)
|
||||
|
||||
@staticmethod
|
||||
def parse_value(value):
|
||||
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
|
Loading…
Reference in New Issue
Block a user