Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CreatedAtTimestamp and fix CreatedAt docs #4765

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion cli/command/formatter/container.go
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we be more explicit as to which format the string would be in?
According to the docs this would be the format.
"2006-01-02 15:04:05.999999999 -0700 MST"

There's also a difference if c.c.Created contains nanoseconds.

withNanoseconds = 2000-02-01 12:13:14.000000015 +0000 UTC
withoutNanoseconds = 2000-02-01 12:13:14 +0000 UTC

https://pkg.go.dev/time#Time.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)
}
Comment on lines +200 to +203
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this function here. It's not being used anywhere right now (except for tests). I would be in favor of just not adding this function since there is no use for it currently.


// RunningFor returns a human-readable representation of the duration for which
// the container has been running.
//
Expand Down
66 changes: 35 additions & 31 deletions cli/command/formatter/container_test.go
Expand Up @@ -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,
"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": "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": "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("")
Expand Down