Merge pull request #701 from graphql-python/feature/date-improvements

Datetime improvements
This commit is contained in:
Syrus Akbary 2018-03-29 22:35:28 -07:00 committed by GitHub
commit d8aacc3b9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 16 deletions

View File

@ -11,6 +11,7 @@ from .types import (
Schema,
Scalar,
String, ID, Int, Float, Boolean,
Date, DateTime, Time,
JSONString,
UUID,
List, NonNull,
@ -54,6 +55,9 @@ __all__ = [
'Float',
'Enum',
'Boolean',
'Date',
'DateTime',
'Time',
'JSONString',
'UUID',
'List',

View File

@ -5,6 +5,7 @@ from .objecttype import ObjectType
from .interface import Interface
from .mutation import Mutation
from .scalars import Scalar, String, ID, Int, Float, Boolean
from .datetime import Date, DateTime, Time
from .json import JSONString
from .uuid import UUID
from .schema import Schema
@ -36,6 +37,9 @@ __all__ = [
'ID',
'Int',
'Float',
'Date',
'DateTime',
'Time',
'JSONString',
'UUID',
'Boolean',

View File

@ -3,17 +3,10 @@ from __future__ import absolute_import
import datetime
from graphql.language import ast
from aniso8601 import parse_datetime, parse_date, parse_time
from .scalars import Scalar
try:
import iso8601
except ImportError:
raise ImportError(
"iso8601 package is required for DateTime Scalar.\n"
"You can install it using: pip install iso8601."
)
class Date(Scalar):
'''
@ -39,8 +32,8 @@ class Date(Scalar):
@staticmethod
def parse_value(value):
try:
return iso8601.parse_date(value).date()
except iso8601.ParseError:
return parse_date(value)
except ValueError:
return None
@ -66,8 +59,8 @@ class DateTime(Scalar):
@staticmethod
def parse_value(value):
try:
return iso8601.parse_date(value)
except iso8601.ParseError:
return parse_datetime(value)
except ValueError:
return None
@ -77,7 +70,6 @@ class Time(Scalar):
specified by
[iso8601](https://en.wikipedia.org/wiki/ISO_8601).
'''
epoch_date = '1970-01-01'
@staticmethod
def serialize(time):
@ -94,7 +86,6 @@ class Time(Scalar):
@classmethod
def parse_value(cls, value):
try:
dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value))
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)
except iso8601.ParseError:
return parse_time(value)
except ValueError:
return None

View File

@ -90,6 +90,7 @@ setup(
'graphql-core>=2.0,<3',
'graphql-relay>=0.4.5,<1',
'promise>=2.1,<3',
'aniso8601>=3,<4',
],
tests_require=tests_require,
extras_require={