github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/graphql/duration.go (about)

     1  package graphql
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	dur "github.com/sosodev/duration"
     8  )
     9  
    10  // UnmarshalDuration returns the duration from a string in ISO8601 format
    11  func UnmarshalDuration(v interface{}) (time.Duration, error) {
    12  	input, ok := v.(string)
    13  	if !ok {
    14  		return 0, fmt.Errorf("input must be a string")
    15  	}
    16  
    17  	d2, err := dur.Parse(input)
    18  	if err != nil {
    19  		return 0, err
    20  	}
    21  	return d2.ToTimeDuration(), nil
    22  }
    23  
    24  // MarshalDuration returns the duration on ISO8601 format
    25  func MarshalDuration(d time.Duration) Marshaler {
    26  	return MarshalString(dur.Format(d))
    27  }