github.com/newrelic/newrelic-client-go@v1.1.0/pkg/synthetics/serialization.go (about)

     1  package synthetics
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  // Time is a type used for unmarshaling timestamps generated by the Synthetics API.
     9  // Its underlying type is time.Time.
    10  type Time time.Time
    11  
    12  var syntheticsTimeFormat = "2006-01-02T15:04:05.999999999-0700"
    13  
    14  // MarshalJSON is responsible for marshaling the Time type.
    15  func (t Time) MarshalJSON() ([]byte, error) {
    16  	dt := time.Time(t).Format(syntheticsTimeFormat)
    17  	return []byte(fmt.Sprintf(`"%s"`, dt)), nil
    18  }
    19  
    20  // UnmarshalJSON is responsible for unmarshaling the Time type.
    21  func (t *Time) UnmarshalJSON(s []byte) (err error) {
    22  	dt := string(s)[1 : len(s)-1]
    23  	parsed, err := time.Parse(syntheticsTimeFormat, dt)
    24  
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	*(*time.Time)(t) = parsed
    30  	return
    31  }
    32  
    33  // Equal provides a comparator for the Time type.
    34  func (t Time) Equal(u Time) bool {
    35  	return time.Time(t).Equal(time.Time(u))
    36  }