Merge pull request #11706 from HuKeping/time

Fix inconsistent date formats in API
This commit is contained in:
Sebastiaan van Stijn
2015-05-13 20:23:19 -07:00
10 changed files with 135 additions and 22 deletions
+2 -2
View File
@@ -47,9 +47,9 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
}
if !*quiet {
if *human {
fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))))
fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(entry.Created)))
} else {
fmt.Fprintf(w, "\t%s\t", time.Unix(entry.Created, 0).Format(time.RFC3339))
fmt.Fprintf(w, "\t%s\t", entry.Created.Format(time.RFC3339))
}
if *noTrunc {
+2 -2
View File
@@ -109,9 +109,9 @@ func (cli *DockerCli) CmdImages(args ...string) error {
if !*quiet {
if *showDigests {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, tag, digest, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, tag, digest, ID, units.HumanDuration(time.Now().UTC().Sub(image.Created)), units.HumanSize(float64(image.VirtualSize)))
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, ID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(image.Created), 0))), units.HumanSize(float64(image.VirtualSize)))
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, ID, units.HumanDuration(time.Now().UTC().Sub(image.Created)), units.HumanSize(float64(image.VirtualSize)))
}
} else {
fmt.Fprintln(w, ID)
+1 -1
View File
@@ -151,7 +151,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", ID, image, command,
units.HumanDuration(time.Now().UTC().Sub(time.Unix(int64(container.Created), 0))),
units.HumanDuration(time.Now().UTC().Sub(container.Created)),
container.Status, api.DisplayablePorts(container.Ports), strings.Join(names, ","))
if *size {
+95
View File
@@ -353,6 +353,37 @@ func (s *Server) getImagesJSON(version version.Version, w http.ResponseWriter, r
return err
}
// For version >= 1.19 the Created filed of image will change
// from int64 to time.Time.
// This is for legacy data format.
if version.LessThan("1.19") {
type legacyImage struct {
ID string `json:"Id"`
ParentId string
RepoTags []string
RepoDigests []string
Created int64
Size int
VirtualSize int
Labels map[string]string
}
legacy := []*legacyImage{}
for _, img := range images {
l := &legacyImage{
ID: img.ID,
ParentId: img.ParentId,
RepoTags: img.RepoTags,
RepoDigests: img.RepoDigests,
Created: img.Created.Unix(),
Size: img.Size,
VirtualSize: img.VirtualSize,
Labels: img.Labels,
}
legacy = append(legacy, l)
}
return writeJSON(w, http.StatusOK, legacy)
}
return writeJSON(w, http.StatusOK, images)
}
@@ -482,6 +513,34 @@ func (s *Server) getImagesHistory(version version.Version, w http.ResponseWriter
return err
}
// For version >= 1.19 the Created filed of image will change
// from int64 to time.Time.
// This is for legacy data format.
if version.LessThan("1.19") {
type legacyImageHistory struct {
ID string `json:"Id"`
Created int64
CreatedBy string
Tags []string
Size int64
Comment string
}
legacy := []*legacyImageHistory{}
for _, img := range history {
l := &legacyImageHistory{
ID: img.ID,
Created: img.Created.Unix(),
CreatedBy: img.CreatedBy,
Tags: img.Tags,
Size: img.Size,
Comment: img.Comment,
}
legacy = append(legacy, l)
}
return writeJSON(w, http.StatusOK, legacy)
}
return writeJSON(w, http.StatusOK, history)
}
@@ -541,6 +600,42 @@ func (s *Server) getContainersJSON(version version.Version, w http.ResponseWrite
return err
}
// For version >= 1.19 the Created filed of container will change
// from int64 to time.Time.
// This is for legacy data format.
if version.LessThan("1.19") {
type legacyContainer struct {
ID string `json:"Id"`
Names []string `json:",omitempty"`
Image string `json:",omitempty"`
Command string `json:",omitempty"`
Created int64 `json:",omitempty"`
Ports []types.Port `json:",omitempty"`
SizeRw int `json:",omitempty"`
SizeRootFs int `json:",omitempty"`
Labels map[string]string `json:",omitempty"`
Status string `json:",omitempty"`
}
legacyContainers := []*legacyContainer{}
for _, c := range containers {
lc := &legacyContainer{
ID: c.ID,
Names: c.Names,
Image: c.Image,
Command: c.Command,
Created: c.Created.Unix(),
Ports: c.Ports,
SizeRw: c.SizeRw,
SizeRootFs: c.SizeRootFs,
Labels: c.Labels,
Status: c.Status,
}
legacyContainers = append(legacyContainers, lc)
}
return writeJSON(w, http.StatusOK, legacyContainers)
}
return writeJSON(w, http.StatusOK, containers)
}
+3 -3
View File
@@ -50,7 +50,7 @@ type ContainerChange struct {
// GET "/images/{name:.*}/history"
type ImageHistory struct {
ID string `json:"Id"`
Created int64
Created time.Time
CreatedBy string
Tags []string
Size int64
@@ -69,7 +69,7 @@ type Image struct {
ParentId string
RepoTags []string
RepoDigests []string
Created int
Created time.Time
Size int
VirtualSize int
Labels map[string]string
@@ -105,7 +105,7 @@ type Container struct {
Names []string `json:",omitempty"`
Image string `json:",omitempty"`
Command string `json:",omitempty"`
Created int `json:",omitempty"`
Created time.Time `json:",omitempty"`
Ports []Port `json:",omitempty"`
SizeRw int `json:",omitempty"`
SizeRootFs int `json:",omitempty"`