Add CreatedAtTimestamp and fix CreatedAt docs

Signed-off-by: Simon H <simon9ha@gmail.com>
This commit is contained in:
Simon H 2024-01-09 09:38:22 -05:00
parent cfe18f5e03
commit bd36415cbe
2 changed files with 39 additions and 30 deletions

View File

@ -192,11 +192,16 @@ func (c *ContainerContext) Command() string {
return strconv.Quote(command)
}
// CreatedAt returns the "Created" date/time of the container as a unix timestamp.
// CreatedAt returns the "Created" date/time of the container as a string
func (c *ContainerContext) CreatedAt() string {
return time.Unix(c.c.Created, 0).String()
}
// CreatedAtTimestamp returns the "Created" date/time of the container as a unix timestamp
func (c *ContainerContext) CreatedAtTimestamp() time.Time {
return time.Unix(c.c.Created, 0)
}
// RunningFor returns a human-readable representation of the duration for which
// the container has been running.
//

View File

@ -336,44 +336,48 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) {
}
func TestContainerContextWriteJSON(t *testing.T) {
unix := time.Now().Add(-65 * time.Second).Unix()
ts := time.Now().Add(-65 * time.Second)
unix := ts.Unix()
containers := []types.Container{
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix, State: "running"},
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix, State: "running"},
}
expectedCreated := time.Unix(unix, 0).String()
expectedCreatedAtTimestamp := ts.Format(time.RFC3339)
expectedJSONs := []map[string]any{
{
"Command": "\"\"",
"CreatedAt": expectedCreated,
"ID": "containerID1",
"Image": "ubuntu",
"Labels": "",
"LocalVolumes": "0",
"Mounts": "",
"Names": "foobar_baz",
"Networks": "",
"Ports": "",
"RunningFor": "About a minute ago",
"Size": "0B",
"State": "running",
"Status": "",
"Command": "\"\"",
"CreatedAt": expectedCreated,
"CreatedAtTimestamp": expectedCreatedAtTimestamp,
"ID": "containerID1",
"Image": "ubuntu",
"Labels": "",
"LocalVolumes": "0",
"Mounts": "",
"Names": "foobar_baz",
"Networks": "",
"Ports": "",
"RunningFor": "About a minute ago",
"Size": "0B",
"State": "running",
"Status": "",
},
{
"Command": "\"\"",
"CreatedAt": expectedCreated,
"ID": "containerID2",
"Image": "ubuntu",
"Labels": "",
"LocalVolumes": "0",
"Mounts": "",
"Names": "foobar_bar",
"Networks": "",
"Ports": "",
"RunningFor": "About a minute ago",
"Size": "0B",
"State": "running",
"Status": "",
"Command": "\"\"",
"CreatedAt": expectedCreated,
"CreatedAtTimestamp": expectedCreatedAtTimestamp,
"ID": "containerID2",
"Image": "ubuntu",
"Labels": "",
"LocalVolumes": "0",
"Mounts": "",
"Names": "foobar_bar",
"Networks": "",
"Ports": "",
"RunningFor": "About a minute ago",
"Size": "0B",
"State": "running",
"Status": "",
},
}
out := bytes.NewBufferString("")