github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/api/v1/time.go (about)

     1  package v1
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  const (
     8  	// APIServerTimeFormat - api-server time lacks the colon in timezone
     9  	APIServerTimeFormat = "2006-01-02T15:04:05.000-0700"
    10  
    11  	// APIServerTimeFormatAlt - api-server time with colon in timezone
    12  	APIServerTimeFormatAlt = "2006-01-02T15:04:05.000-07:00"
    13  )
    14  
    15  // Time - time
    16  type Time time.Time
    17  
    18  // UnmarshalJSON - unmarshal json for time
    19  func (t *Time) UnmarshalJSON(bytes []byte) error {
    20  	tt, err := time.Parse(`"`+APIServerTimeFormat+`"`, string(bytes))
    21  
    22  	if err == nil {
    23  		*t = Time(tt)
    24  		return nil
    25  	}
    26  	tt, err = time.Parse(`"`+APIServerTimeFormatAlt+`"`, string(bytes))
    27  	if err != nil {
    28  		return err
    29  	}
    30  	*t = Time(tt)
    31  	return nil
    32  }
    33  
    34  // MarshalJSON -
    35  func (t Time) MarshalJSON() ([]byte, error) {
    36  	tt := time.Time(t)
    37  	b := make([]byte, 0, len(APIServerTimeFormat)+2)
    38  	b = append(b, '"')
    39  	b = tt.AppendFormat(b, APIServerTimeFormat)
    40  	b = append(b, '"')
    41  	return b, nil
    42  }