Move timeutils functions to the only places where they are used.
- Move time json marshaling to the jsonlog package: this is a docker internal hack that we should not promote as a library. - Move Timestamp encoding/decoding functions to the API types: This is only used there. It could be a standalone library but I don't this it's worth having a separated repo for this. It could introduce more complexity than it solves. Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
parent
202e73c7ad
commit
0585b88aee
7 changed files with 25 additions and 246 deletions
|
@ -13,8 +13,6 @@
|
|||
// "bytes"
|
||||
//-
|
||||
// "unicode/utf8"
|
||||
//+
|
||||
//+ "github.com/docker/docker/pkg/timeutils"
|
||||
// )
|
||||
//
|
||||
// func (mj *JSONLog) MarshalJSON() ([]byte, error) {
|
||||
|
@ -43,7 +41,7 @@
|
|||
// }
|
||||
// buf.WriteString(`"time":`)
|
||||
//- obj, err = mj.Created.MarshalJSON()
|
||||
//+ timestamp, err = timeutils.FastMarshalJSON(mj.Created)
|
||||
//+ timestamp, err = FastTimeMarshalJSON(mj.Created)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
@ -69,8 +67,6 @@ package jsonlog
|
|||
import (
|
||||
"bytes"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/docker/docker/pkg/timeutils"
|
||||
)
|
||||
|
||||
// MarshalJSON marshals the JSONLog.
|
||||
|
@ -111,7 +107,7 @@ func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
|
|||
buf.WriteString(`,`)
|
||||
}
|
||||
buf.WriteString(`"time":`)
|
||||
timestamp, err = timeutils.FastMarshalJSON(mj.Created)
|
||||
timestamp, err = FastTimeMarshalJSON(mj.Created)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
27
jsonlog/time_marshalling.go
Normal file
27
jsonlog/time_marshalling.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Package jsonlog provides helper functions to parse and print time (time.Time) as JSON.
|
||||
package jsonlog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// RFC3339NanoFixed is our own version of RFC339Nano because we want one
|
||||
// that pads the nano seconds part with zeros to ensure
|
||||
// the timestamps are aligned in the logs.
|
||||
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
|
||||
// JSONFormat is the format used by FastMarshalJSON
|
||||
JSONFormat = `"` + time.RFC3339Nano + `"`
|
||||
)
|
||||
|
||||
// FastTimeMarshalJSON avoids one of the extra allocations that
|
||||
// time.MarshalJSON is making.
|
||||
func FastTimeMarshalJSON(t time.Time) (string, error) {
|
||||
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.
|
||||
return "", errors.New("time.MarshalJSON: year outside of range [0,9999]")
|
||||
}
|
||||
return t.Format(JSONFormat), nil
|
||||
}
|
47
jsonlog/time_marshalling_test.go
Normal file
47
jsonlog/time_marshalling_test.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package jsonlog
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Testing to ensure 'year' fields is between 0 and 9999
|
||||
func TestFastTimeMarshalJSONWithInvalidDate(t *testing.T) {
|
||||
aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
|
||||
json, err := FastTimeMarshalJSON(aTime)
|
||||
if err == nil {
|
||||
t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
|
||||
}
|
||||
anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
|
||||
json, err = FastTimeMarshalJSON(anotherTime)
|
||||
if err == nil {
|
||||
t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFastTimeMarshalJSON(t *testing.T) {
|
||||
aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
|
||||
json, err := FastTimeMarshalJSON(aTime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := "\"2015-05-29T11:01:02.000000003Z\""
|
||||
if json != expected {
|
||||
t.Fatalf("Expected %v, got %v", expected, json)
|
||||
}
|
||||
|
||||
location, err := time.LoadLocation("Europe/Paris")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
|
||||
json, err = FastTimeMarshalJSON(aTime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected = "\"2015-05-29T11:01:02.000000003+02:00\""
|
||||
if json != expected {
|
||||
t.Fatalf("Expected %v, got %v", expected, json)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue