github.com/greenboxal/deis@v1.12.1/pkg/time/time.go (about)

     1  package time
     2  
     3  import "time"
     4  
     5  // DeisDatetimeFormat is the standard date/time representation used in Deis.
     6  const DeisDatetimeFormat string = "2006-01-02T15:04:05MST"
     7  
     8  // Time represents the standard datetime format used across the Deis Platform.
     9  type Time struct {
    10  	time.Time
    11  }
    12  
    13  func (t *Time) format() string {
    14  	return t.Time.Format(DeisDatetimeFormat)
    15  }
    16  
    17  // MarshalJSON implements the json.Marshaler interface.
    18  // The time is a quoted string in Deis' datetime format.
    19  func (t *Time) MarshalJSON() ([]byte, error) {
    20  	return []byte(t.Time.Format(`"` + DeisDatetimeFormat + `"`)), nil
    21  }
    22  
    23  // UnmarshalText implements the encoding.TextUnmarshaler interface.
    24  // The time is expected to be in Deis' datetime format.
    25  func (t *Time) UnmarshalText(data []byte) (err error) {
    26  	tt, err := time.Parse(DeisDatetimeFormat, string(data))
    27  	*t = Time{tt}
    28  	return
    29  }
    30  
    31  // UnmarshalJSON implements the json.Unmarshaler interface.
    32  // The time is expected to be a quoted string in Deis' datetime format.
    33  func (t *Time) UnmarshalJSON(data []byte) (err error) {
    34  	// Fractional seconds are handled implicitly by Parse.
    35  	tt, err := time.Parse(`"`+DeisDatetimeFormat+`"`, string(data))
    36  	*t = Time{tt}
    37  	return
    38  }