...
1 package graphql
2
3 import (
4 "errors"
5 "io"
6 "strconv"
7 "time"
8 )
9
10 func 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.RFC3339Nano)))
17 })
18 }
19
20 func UnmarshalTime(v interface{}) (time.Time, error) {
21 if tmpStr, ok := v.(string); ok {
22 return time.Parse(time.RFC3339Nano, tmpStr)
23 }
24 return time.Time{}, errors.New("time should be RFC3339Nano formatted string")
25 }
26
View as plain text