Browse code

add the timeutils package

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)

unclejack authored on 2014/09/16 03:44:58
Showing 7 changed files
... ...
@@ -34,6 +34,7 @@ import (
34 34
 	"github.com/docker/docker/pkg/parsers/filters"
35 35
 	"github.com/docker/docker/pkg/signal"
36 36
 	"github.com/docker/docker/pkg/term"
37
+	"github.com/docker/docker/pkg/timeutils"
37 38
 	"github.com/docker/docker/pkg/units"
38 39
 	"github.com/docker/docker/registry"
39 40
 	"github.com/docker/docker/runconfig"
... ...
@@ -1650,7 +1651,7 @@ func (cli *DockerCli) CmdEvents(args ...string) error {
1650 1650
 		loc = time.FixedZone(time.Now().Zone())
1651 1651
 	)
1652 1652
 	var setTime = func(key, value string) {
1653
-		format := utils.RFC3339NanoFixed
1653
+		format := timeutils.RFC3339NanoFixed
1654 1654
 		if len(value) < len(format) {
1655 1655
 			format = format[:len(value)]
1656 1656
 		}
... ...
@@ -8,12 +8,11 @@ import (
8 8
 	"os"
9 9
 	"strconv"
10 10
 
11
-	"github.com/docker/docker/pkg/log"
12
-	"github.com/docker/docker/pkg/tailfile"
13
-
14 11
 	"github.com/docker/docker/engine"
15 12
 	"github.com/docker/docker/pkg/jsonlog"
16
-	"github.com/docker/docker/utils"
13
+	"github.com/docker/docker/pkg/log"
14
+	"github.com/docker/docker/pkg/tailfile"
15
+	"github.com/docker/docker/pkg/timeutils"
17 16
 )
18 17
 
19 18
 func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
... ...
@@ -35,7 +34,7 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
35 35
 		return job.Errorf("You must choose at least one stream")
36 36
 	}
37 37
 	if times {
38
-		format = utils.RFC3339NanoFixed
38
+		format = timeutils.RFC3339NanoFixed
39 39
 	}
40 40
 	if tail == "" {
41 41
 		tail = "all"
... ...
@@ -8,7 +8,7 @@ import (
8 8
 	"testing"
9 9
 	"time"
10 10
 
11
-	"github.com/docker/docker/utils"
11
+	"github.com/docker/docker/pkg/timeutils"
12 12
 )
13 13
 
14 14
 // This used to work, it test a log of PageSize-1 (gh#4851)
... ...
@@ -104,7 +104,7 @@ func TestLogsTimestamps(t *testing.T) {
104 104
 
105 105
 	for _, l := range lines {
106 106
 		if l != "" {
107
-			_, err := time.Parse(utils.RFC3339NanoFixed+" ", ts.FindString(l))
107
+			_, err := time.Parse(timeutils.RFC3339NanoFixed+" ", ts.FindString(l))
108 108
 			if err != nil {
109 109
 				t.Fatalf("Failed to parse timestamp from %v: %v", l, err)
110 110
 			}
111 111
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+Cristian Staretu <cristian.staretu@gmail.com> (@unclejack)
0 1
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+package timeutils
1
+
2
+import (
3
+	"errors"
4
+	"time"
5
+)
6
+
7
+const (
8
+	// Define our own version of RFC339Nano because we want one
9
+	// that pads the nano seconds part with zeros to ensure
10
+	// the timestamps are aligned in the logs.
11
+	RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
12
+	JSONFormat       = `"` + time.RFC3339Nano + `"`
13
+)
14
+
15
+func FastMarshalJSON(t time.Time) (string, error) {
16
+	if y := t.Year(); y < 0 || y >= 10000 {
17
+		// RFC 3339 is clear that years are 4 digits exactly.
18
+		// See golang.org/issue/4556#c15 for more discussion.
19
+		return "", errors.New("Time.MarshalJSON: year outside of range [0,9999]")
20
+	}
21
+	return t.Format(JSONFormat), nil
22
+}
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"time"
9 9
 
10 10
 	"github.com/docker/docker/pkg/term"
11
+	"github.com/docker/docker/pkg/timeutils"
11 12
 	"github.com/docker/docker/pkg/units"
12 13
 )
13 14
 
... ...
@@ -100,7 +101,7 @@ func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
100 100
 		return nil
101 101
 	}
102 102
 	if jm.Time != 0 {
103
-		fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(RFC3339NanoFixed))
103
+		fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(timeutils.RFC3339NanoFixed))
104 104
 	}
105 105
 	if jm.ID != "" {
106 106
 		fmt.Fprintf(out, "%s: ", jm.ID)
... ...
@@ -24,13 +24,6 @@ import (
24 24
 	"github.com/docker/docker/pkg/log"
25 25
 )
26 26
 
27
-const (
28
-	// Define our own version of RFC339Nano because we want one
29
-	// that pads the nano seconds part with zeros to ensure
30
-	// the timestamps are aligned in the logs.
31
-	RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
32
-)
33
-
34 27
 type KeyValuePair struct {
35 28
 	Key   string
36 29
 	Value string