sgqlc.types.datetime module¶
GraphQL Types for datetime¶
Maps:
datetime.timeto GraphQLTime
datetime.dateto GraphQLDate
datetime.datetimeto GraphQLDateTime
You may either explicitly use this module classes or datetime,
as they will be automatically recognized by the framework.
Conversions assume ISO 8601 encoding.
Examples¶
>>> from sgqlc.types import Type
>>> class MyDateTimeType(Type):
... time1 = datetime.time
... time2 = Time
... date1 = datetime.date
... date2 = Date
... datetime1 = datetime.datetime
... datetime2 = DateTime
...
>>> MyDateTimeType # or repr() to print out GraphQL
type MyDateTimeType {
time1: Time
time2: Time
date1: Date
date2: Date
datetime1: DateTime
datetime2: DateTime
}
>>> json_data = {
... 'time1': '12:34:56',
... 'time2': '12:34:56-03:00', # set timezone GMT-3h
... 'date1': '2018-01-02',
... 'date2': '20180102', # compact form is accepted
... 'datetime1': '2018-01-02T12:34:56Z', # Z = GMT/UTC
... 'datetime2': '20180102T123456-0300', # compact form is accepted
... }
>>> obj = MyDateTimeType(json_data)
>>> for field_name in obj:
... print(field_name, repr(obj[field_name]))
time1 datetime.time(12, 34, 56)
time2 datetime.time(12, 34, 56, tzinfo=...(...-1, ...75600)))
date1 datetime.date(2018, 1, 2)
date2 datetime.date(2018, 1, 2)
datetime1 datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=....utc)
datetime2 datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=...75600)))
Pre-converted types are allowed:
>>> json_data = { 'time1': datetime.time(12, 34, 56) }
>>> obj = MyDateTimeType(json_data)
>>> obj.time1
datetime.time(12, 34, 56)
However, invalid encoded strings are not:
>>> json_data = { 'time1': '12-3' }
>>> obj = MyDateTimeType(json_data)
Traceback (most recent call last):
...
ValueError: MyDateTimeType selection 'time1': ...
>>> json_data = { 'date1': '12-3' }
>>> obj = MyDateTimeType(json_data)
Traceback (most recent call last):
...
ValueError: MyDateTimeType selection 'date1': ...
>>> json_data = { 'datetime1': '2018-01-02X12-34-56Z' }
>>> obj = MyDateTimeType(json_data)
Traceback (most recent call last):
...
ValueError: MyDateTimeType selection 'datetime1': ...
- license:
ISC
- class sgqlc.types.datetime.Date(json_data, selection_list=None)[source]¶
Bases:
ScalarDate encoded as string using ISO8601 (YYYY-MM-SS)
While not a standard GraphQL type, it’s often used, so expose to make life simpler.
>>> Date('2018-01-02') datetime.date(2018, 1, 2) >>> Date('20180102') # compact form datetime.date(2018, 1, 2)
Pre-converted values are allowed:
>>> Date(datetime.date(2018, 1, 2)) datetime.date(2018, 1, 2)
It can also serialize to JSON:
>>> Date.__to_json_value__(datetime.date(2018, 1, 2)) '2018-01-02' >>> Date.__to_json_value__('2018-01-02') '2018-01-02' >>> Date.__to_json_value__(None)
- class sgqlc.types.datetime.DateTime(json_data, selection_list=None)[source]¶
Bases:
ScalarDate and Time encoded as string using ISO8601 (YYYY-mm-ddTHH:MM:SS[.mmm][+/-HH:MM])
While not a standard GraphQL type, it’s often used, so expose to make life simpler.
>>> DateTime('2018-01-02T12:34:56') # naive, no timezone datetime.datetime(2018, 1, 2, 12, 34, 56) >>> DateTime('2018-01-02T12:34:56Z') # Z = GMT/UTC datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=datetime.timezone.utc) >>> DateTime('2018-01-02T12:34:56-05:30') datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=..., ...70200))) >>> DateTime('2018-01-02T12:34:56+05:30') datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=...(...19800))) >>> DateTime('20180102T123456') # compact form datetime.datetime(2018, 1, 2, 12, 34, 56) >>> DateTime('20180102T123456Z') # compact form, GMT/UTC datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=datetime.timezone.utc) >>> DateTime('20180102T123456-0530') datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=..., ...70200))) >>> DateTime('20180102T123456+0530') datetime.datetime(2018, 1, 2, 12, 34, 56, tzinfo=...(...19800)))
Pre-converted values are allowed:
>>> DateTime(datetime.datetime(2018, 1, 2, 12, 34, 56)) datetime.datetime(2018, 1, 2, 12, 34, 56)
It can also serialize to JSON:
>>> dt = datetime.datetime(2018, 1, 2, 12, 34, 56) >>> DateTime.__to_json_value__(dt) '2018-01-02T12:34:56' >>> DateTime.__to_json_value__('2018-01-02T12:34:56') '2018-01-02T12:34:56' >>> tzinfo = datetime.timezone.utc >>> DateTime.__to_json_value__(dt.replace(tzinfo=tzinfo)) '2018-01-02T12:34:56+00:00' >>> DateTime.__to_json_value__(None)
- class sgqlc.types.datetime.Time(json_data, selection_list=None)[source]¶
Bases:
ScalarTime encoded as string using ISO8601 (HH:MM:SS[.mmm][+/-HH:MM])
While not a standard GraphQL type, it’s often used, so expose to make life simpler.
>>> Time('12:34:56') # naive, no timezone datetime.time(12, 34, 56) >>> Time('12:34:56Z') # Z = GMT/UTC datetime.time(12, 34, 56, tzinfo=datetime.timezone.utc) >>> Time('12:34:56-05:30') datetime.time(12, 34, 56, tzinfo=...(...-1, ...70200))) >>> Time('12:34:56+05:30') datetime.time(12, 34, 56, tzinfo=...(...19800))) >>> Time('123456') # compact form datetime.time(12, 34, 56) >>> Time('123456Z') # compact form, GMT/UTC datetime.time(12, 34, 56, tzinfo=datetime.timezone.utc) >>> Time('123456-0530') datetime.time(12, 34, 56, tzinfo=...(...-1, ...70200))) >>> Time('123456+0530') datetime.time(12, 34, 56, tzinfo=...(...19800)))
Pre-converted values are allowed:
>>> Time(datetime.time(12, 34, 56)) datetime.time(12, 34, 56)
It can also serialize to JSON:
>>> Time.__to_json_value__(datetime.time(12, 34, 56)) '12:34:56' >>> tzinfo = datetime.timezone.utc >>> Time.__to_json_value__(datetime.time(12, 34, 56, 0, tzinfo)) '12:34:56+00:00' >>> Time.__to_json_value__('12:34:56') '12:34:56' >>> Time.__to_json_value__(None)