File size: 693 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package graphql2

import (
	"io"
	"strings"
	"time"

	graphql "github.com/99designs/gqlgen/graphql"
	"github.com/pkg/errors"
	"github.com/target/goalert/validation"
)

func MarshalISOTimestamp(t time.Time) graphql.Marshaler {
	return graphql.WriterFunc(func(w io.Writer) {
		if t.IsZero() {
			_, _ = io.WriteString(w, "null")
			return
		}
		_, _ = io.WriteString(w, `"`+t.UTC().Format(time.RFC3339Nano)+`"`)
	})
}
func UnmarshalISOTimestamp(v interface{}) (time.Time, error) {
	str, ok := v.(string)
	if !ok {
		return time.Time{}, errors.New("timestamps must be strings")
	}
	str = strings.Trim(str, `"`)

	t, err := time.Parse(time.RFC3339Nano, str)
	return t, validation.WrapError(err)
}