2015-12-15 14:49:41 -05:00
|
|
|
package jsonlog
|
2014-09-15 21:44:58 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-09-22 14:40:10 -04:00
|
|
|
"github.com/pkg/errors"
|
2014-09-15 21:44:58 +03:00
|
|
|
)
|
|
|
|
|
2017-09-22 14:40:10 -04:00
|
|
|
const jsonFormat = `"` + time.RFC3339Nano + `"`
|
|
|
|
|
2017-09-22 15:37:16 -04:00
|
|
|
// fastTimeMarshalJSON avoids one of the extra allocations that
|
2014-10-06 22:27:56 +03:00
|
|
|
// time.MarshalJSON is making.
|
2017-09-22 15:37:16 -04:00
|
|
|
func fastTimeMarshalJSON(t time.Time) (string, error) {
|
2014-09-15 21:44:58 +03:00
|
|
|
if y := t.Year(); y < 0 || y >= 10000 {
|
|
|
|
// RFC 3339 is clear that years are 4 digits exactly.
|
|
|
|
// See golang.org/issue/4556#c15 for more discussion.
|
2014-10-06 22:27:56 +03:00
|
|
|
return "", errors.New("time.MarshalJSON: year outside of range [0,9999]")
|
2014-09-15 21:44:58 +03:00
|
|
|
}
|
2017-09-22 14:40:10 -04:00
|
|
|
return t.Format(jsonFormat), nil
|
2014-09-15 21:44:58 +03:00
|
|
|
}
|