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