time.go

 1package graphql
 2
 3import (
 4	"errors"
 5	"io"
 6	"strconv"
 7	"time"
 8)
 9
10func MarshalTime(t time.Time) Marshaler {
11	if t.IsZero() {
12		return Null
13	}
14
15	return WriterFunc(func(w io.Writer) {
16		io.WriteString(w, strconv.Quote(t.Format(time.RFC3339)))
17	})
18}
19
20func UnmarshalTime(v interface{}) (time.Time, error) {
21	if tmpStr, ok := v.(string); ok {
22		return time.Parse(time.RFC3339, tmpStr)
23	}
24	return time.Time{}, errors.New("time should be RFC3339 formatted string")
25}