Skip to content

Instantly share code, notes, and snippets.

@richmondwang
Last active March 31, 2017 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richmondwang/3a6ae068d205981720817a0e82d71394 to your computer and use it in GitHub Desktop.
Save richmondwang/3a6ae068d205981720817a0e82d71394 to your computer and use it in GitHub Desktop.
This is a custom Scalar type. This DateTime Scalar can be formatted into anything the consumer needs it to be.
# -*- coding: utf-8 -*-
import datetime
import graphene
from graphene.types.datetime import DateTime
class FormattedDateTime(DateTime):
"""
This is a custom Scalar type. This DateTime Scalar can be formatted into
anything the consumer needs it to be.
"""
__format_description__ = """
Default is in [iso8601](https://en.wikipedia.org/wiki/ISO_8601) format.
====== ================================================================================================ ==========================
Code Meaning Example
====== ================================================================================================ ==========================
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 1
%d Day of the month as a zero-padded decimal number. 30
%-d Day of the month as a decimal number. (Platform specific) 30
%b Month as locale’s abbreviated name. Sep
%B Month as locale’s full name. September
%m Month as a zero-padded decimal number. 9
%-m Month as a decimal number. (Platform specific) 9
%y Year without century as a zero-padded decimal number. 13
%Y Year with century as a decimal number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 7
%-H Hour (24-hour clock) as a decimal number. (Platform specific) 7
%I Hour (12-hour clock) as a zero-padded decimal number. 7
%-I Hour (12-hour clock) as a decimal number. (Platform specific) 7
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded decimal number. 6
%-M Minute as a decimal number. (Platform specific) 6
%S Second as a zero-padded decimal number. 5
%-S Second as a decimal number. (Platform specific) 5
%f Microsecond as a decimal number, zero-padded on the left. 0
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
%Z Time zone name (empty string if the object is naive).
%j Day of the year as a zero-padded decimal number. 273
%-j Day of the year as a decimal number. (Platform specific) 273
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. 39
%W Week number of the year (Monday as the first day of the week) as a decimal number. 39
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 09/30/13
%X Locale’s appropriate time representation. 7:06:05
%% A literal '%' character. %
====== ================================================================================================ ==========================
Please see [Strftime.org](http://strftime.org/) for more details.
"""
def __init__(self, *args, **kwargs):
kwargs['fmt'] = graphene.Argument(
graphene.String,
description='The format of the DateTime you want be returned.' +
self.__format_description__)
kwargs['tz'] = graphene.Argument(
graphene.String,
description='The Time Zone you want the DateTime to be in.')
super().__init__(*args, **kwargs)
@staticmethod
def serialize(dt):
if isinstance(dt, (datetime.datetime, datetime.date)):
return dt.isoformat()
return dt
# sample query
"""
{
me {
username
givenName
familyName
email
createdAt(fmt: "Registred on %a %d, %m %Y %H:%M%:S", tz='JST')
updatedAt(fmt: "Updated on %a %d, %m %Y %H:%M%:S", tz='JST')
}
}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment