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) 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 { func (c *ContainerContext) CreatedAt() string {
return time.Unix(c.c.Created, 0).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 // RunningFor returns a human-readable representation of the duration for which
// the container has been running. // the container has been running.
// //

View File

@ -336,16 +336,19 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) {
} }
func TestContainerContextWriteJSON(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{ containers := []types.Container{
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix, State: "running"}, {ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix, State: "running"},
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix, State: "running"}, {ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix, State: "running"},
} }
expectedCreated := time.Unix(unix, 0).String() expectedCreated := time.Unix(unix, 0).String()
expectedCreatedAtTimestamp := ts.Format(time.RFC3339)
expectedJSONs := []map[string]any{ expectedJSONs := []map[string]any{
{ {
"Command": "\"\"", "Command": "\"\"",
"CreatedAt": expectedCreated, "CreatedAt": expectedCreated,
"CreatedAtTimestamp": expectedCreatedAtTimestamp,
"ID": "containerID1", "ID": "containerID1",
"Image": "ubuntu", "Image": "ubuntu",
"Labels": "", "Labels": "",
@ -362,6 +365,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
{ {
"Command": "\"\"", "Command": "\"\"",
"CreatedAt": expectedCreated, "CreatedAt": expectedCreated,
"CreatedAtTimestamp": expectedCreatedAtTimestamp,
"ID": "containerID2", "ID": "containerID2",
"Image": "ubuntu", "Image": "ubuntu",
"Labels": "", "Labels": "",