time_marshalling.go 602 B

1234567891011121314151617181920
  1. package jsonlog // import "github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog"
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. )
  6. const jsonFormat = `"` + time.RFC3339Nano + `"`
  7. // fastTimeMarshalJSON avoids one of the extra allocations that
  8. // time.MarshalJSON is making.
  9. func fastTimeMarshalJSON(t time.Time) (string, error) {
  10. if y := t.Year(); y < 0 || y >= 10000 {
  11. // RFC 3339 is clear that years are 4 digits exactly.
  12. // See golang.org/issue/4556#c15 for more discussion.
  13. return "", errors.New("time.MarshalJSON: year outside of range [0,9999]")
  14. }
  15. return t.Format(jsonFormat), nil
  16. }