results.go raw

   1  package gnocchi
   2  
   3  import (
   4  	"bytes"
   5  	"encoding/json"
   6  	"time"
   7  )
   8  
   9  // RFC3339NanoTimezone describes a common timestamp format used by Gnocchi API responses.
  10  const RFC3339NanoTimezone = "2006-01-02T15:04:05.999999+00:00"
  11  
  12  // RFC3339NanoNoTimezone describes a common timestamp format that can be used for Gnocchi requests
  13  // with time ranges.
  14  const RFC3339NanoNoTimezone = "2006-01-02T15:04:05.999999"
  15  
  16  // JSONRFC3339NanoTimezone is a type for Gnocchi responses timestamps with a timezone offset.
  17  type JSONRFC3339NanoTimezone time.Time
  18  
  19  // UnmarshalJSON helps to unmarshal timestamps from Gnocchi responses to the
  20  // JSONRFC3339NanoTimezone type.
  21  func (jt *JSONRFC3339NanoTimezone) UnmarshalJSON(data []byte) error {
  22  	b := bytes.NewBuffer(data)
  23  	dec := json.NewDecoder(b)
  24  	var s string
  25  	if err := dec.Decode(&s); err != nil {
  26  		return err
  27  	}
  28  	if s == "" {
  29  		return nil
  30  	}
  31  	t, err := time.Parse(RFC3339NanoTimezone, s)
  32  	if err != nil {
  33  		return err
  34  	}
  35  	*jt = JSONRFC3339NanoTimezone(t)
  36  	return nil
  37  }
  38