github.com/wlan0/docker@v1.5.0/pkg/timeutils/json.go (about) 1 package timeutils 2 3 import ( 4 "errors" 5 "time" 6 ) 7 8 const ( 9 // RFC3339NanoFixed is our own version of RFC339Nano because we want one 10 // that pads the nano seconds part with zeros to ensure 11 // the timestamps are aligned in the logs. 12 RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00" 13 // JSONFormat is the format used by FastMarshalJSON 14 JSONFormat = `"` + time.RFC3339Nano + `"` 15 ) 16 17 // FastMarshalJSON avoids one of the extra allocations that 18 // time.MarshalJSON is making. 19 func FastMarshalJSON(t time.Time) (string, error) { 20 if y := t.Year(); y < 0 || y >= 10000 { 21 // RFC 3339 is clear that years are 4 digits exactly. 22 // See golang.org/issue/4556#c15 for more discussion. 23 return "", errors.New("time.MarshalJSON: year outside of range [0,9999]") 24 } 25 return t.Format(JSONFormat), nil 26 }