mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 04:07:16 +03:00
Merge pull request #701 from graphql-python/feature/date-improvements
Datetime improvements
This commit is contained in:
commit
d8aacc3b9e
|
@ -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',
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user