mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-23 10:03:54 +03:00
Merge pull request #152 from jdugan1024/improve-iso8601-handling
improve iso8601 handling
This commit is contained in:
commit
6272057997
|
@ -1,5 +1,5 @@
|
||||||
import datetime
|
|
||||||
import json
|
import json
|
||||||
|
import iso8601
|
||||||
|
|
||||||
from graphql.core.language import ast
|
from graphql.core.language import ast
|
||||||
|
|
||||||
|
@ -33,9 +33,8 @@ class DateTime(Scalar):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_literal(node):
|
def parse_literal(node):
|
||||||
if isinstance(node, ast.StringValue):
|
if isinstance(node, ast.StringValue):
|
||||||
return datetime.datetime.strptime(
|
return iso8601.parse_date(node.value)
|
||||||
node.value, "%Y-%m-%dT%H:%M:%S.%f")
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_value(value):
|
def parse_value(value):
|
||||||
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
|
return iso8601.parse_date(value)
|
||||||
|
|
27
graphene/core/types/tests/test_custom_scalars.py
Normal file
27
graphene/core/types/tests/test_custom_scalars.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import iso8601
|
||||||
|
|
||||||
|
from graphql.core.language.ast import StringValue
|
||||||
|
|
||||||
|
from ..custom_scalars import DateTime
|
||||||
|
|
||||||
|
|
||||||
|
def test_date_time():
|
||||||
|
test_iso_string = "2016-04-29T18:34:12.502Z"
|
||||||
|
|
||||||
|
def check_datetime(test_dt):
|
||||||
|
assert test_dt.tzinfo == iso8601.UTC
|
||||||
|
assert test_dt.year == 2016
|
||||||
|
assert test_dt.month == 4
|
||||||
|
assert test_dt.day == 29
|
||||||
|
assert test_dt.hour == 18
|
||||||
|
assert test_dt.minute == 34
|
||||||
|
assert test_dt.second == 12
|
||||||
|
|
||||||
|
test_dt = DateTime().parse_value(test_iso_string)
|
||||||
|
check_datetime(test_dt)
|
||||||
|
|
||||||
|
assert DateTime.serialize(test_dt) == "2016-04-29T18:34:12.502000+00:00"
|
||||||
|
|
||||||
|
node = StringValue(test_iso_string)
|
||||||
|
test_dt = DateTime.parse_literal(node)
|
||||||
|
check_datetime(test_dt)
|
Loading…
Reference in New Issue
Block a user