github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/graphql/time.go (about)

     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.RFC3339)))
    17  	})
    18  }
    19  
    20  func 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  }