Skip to content

Commit c9207bc

Browse files
committed
Format times in inspect command with a template as RFC3339Nano
In 1.6.2 we were decoding inspect API response into interface{}. time.Time fields were JSON encoded as RFC3339Nano in the response and when decoded into interface{} they were just strings so the inspect template treated them as just strings. From 1.7 we are decoding into types.ContainerJSON and when the template gets executed it now gets a time.Time and it's formatted as 2015-07-22 05:02:38.091530369 +0000 UTC. This patch brings back the old behavior by typing time.Time fields as string so they gets formatted as they were encoded in JSON -- RCF3339Nano Signed-off-by: Antonio Murdaca <[email protected]>
1 parent c051ed7 commit c9207bc

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

api/types/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type ImageInspect struct {
8686
Id string
8787
Parent string
8888
Comment string
89-
Created time.Time
89+
Created string
9090
Container string
9191
ContainerConfig *runconfig.Config
9292
DockerVersion string
@@ -215,14 +215,14 @@ type ContainerState struct {
215215
Pid int
216216
ExitCode int
217217
Error string
218-
StartedAt time.Time
219-
FinishedAt time.Time
218+
StartedAt string
219+
FinishedAt string
220220
}
221221

222222
// GET "/containers/{name:.*}/json"
223223
type ContainerJSONBase struct {
224224
Id string
225-
Created time.Time
225+
Created string
226226
Path string
227227
Args []string
228228
State *ContainerState

daemon/inspect.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package daemon
22

33
import (
44
"fmt"
5+
"time"
56

67
"github.com/docker/docker/api/types"
78
)
@@ -91,13 +92,13 @@ func (daemon *Daemon) getInspectData(container *Container) (*types.ContainerJSON
9192
Pid: container.State.Pid,
9293
ExitCode: container.State.ExitCode,
9394
Error: container.State.Error,
94-
StartedAt: container.State.StartedAt,
95-
FinishedAt: container.State.FinishedAt,
95+
StartedAt: container.State.StartedAt.Format(time.RFC3339Nano),
96+
FinishedAt: container.State.FinishedAt.Format(time.RFC3339Nano),
9697
}
9798

9899
contJSONBase := &types.ContainerJSONBase{
99100
Id: container.ID,
100-
Created: container.Created,
101+
Created: container.Created.Format(time.RFC3339Nano),
101102
Path: container.Path,
102103
Args: container.Args,
103104
State: containerState,

graph/service.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"runtime"
7+
"time"
78

89
"github.com/Sirupsen/logrus"
910
"github.com/docker/docker/api/types"
@@ -34,7 +35,7 @@ func (s *TagStore) Lookup(name string) (*types.ImageInspect, error) {
3435
Id: image.ID,
3536
Parent: image.Parent,
3637
Comment: image.Comment,
37-
Created: image.Created,
38+
Created: image.Created.Format(time.RFC3339Nano),
3839
Container: image.Container,
3940
ContainerConfig: &image.ContainerConfig,
4041
DockerVersion: image.DockerVersion,

integration-cli/docker_cli_inspect_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os/exec"
66
"strconv"
77
"strings"
8+
"time"
89

910
"github.com/docker/docker/api/types"
1011
"github.com/go-check/check"
@@ -260,3 +261,28 @@ func (s *DockerSuite) TestInspectBindMountPoint(c *check.C) {
260261
c.Fatalf("Expected rw to be false")
261262
}
262263
}
264+
265+
// #14947
266+
func (s *DockerSuite) TestInspectTimesAsRFC3339Nano(c *check.C) {
267+
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
268+
id := strings.TrimSpace(out)
269+
startedAt, err := inspectField(id, "State.StartedAt")
270+
c.Assert(err, check.IsNil)
271+
finishedAt, err := inspectField(id, "State.FinishedAt")
272+
c.Assert(err, check.IsNil)
273+
created, err := inspectField(id, "Created")
274+
c.Assert(err, check.IsNil)
275+
276+
_, err = time.Parse(time.RFC3339Nano, startedAt)
277+
c.Assert(err, check.IsNil)
278+
_, err = time.Parse(time.RFC3339Nano, finishedAt)
279+
c.Assert(err, check.IsNil)
280+
_, err = time.Parse(time.RFC3339Nano, created)
281+
c.Assert(err, check.IsNil)
282+
283+
created, err = inspectField("busybox", "Created")
284+
c.Assert(err, check.IsNil)
285+
286+
_, err = time.Parse(time.RFC3339Nano, created)
287+
c.Assert(err, check.IsNil)
288+
}

0 commit comments

Comments
 (0)