Skip to content

Commit

Permalink
Merge pull request #3778 from imjasonh/created
Browse files Browse the repository at this point in the history
cli: hide old CREATED times, instead of "52 years ago"
  • Loading branch information
thaJeztah authored Sep 16, 2022
2 parents 9ac8584 + 0253634 commit 06cbd26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cli/command/image/formatter_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ func (c *historyContext) CreatedAt() string {
return time.Unix(c.h.Created, 0).Format(time.RFC3339)
}

// epoch is the time before which created-at dates are not displayed with human units.
var epoch = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC).Unix()

func (c *historyContext) CreatedSince() string {
if !c.human {
return c.CreatedAt()
}
if c.h.Created <= epoch {
return ""
}
created := units.HumanDuration(time.Now().UTC().Sub(time.Unix(c.h.Created, 0)))
return created + " ago"
}
Expand Down
31 changes: 31 additions & 0 deletions cli/command/image/formatter_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ func TestHistoryContext_CreatedSince(t *testing.T) {
human: false,
}, dateStr, ctx.CreatedSince,
},
{
// The zero time is not displayed.
historyContext{
h: image.HistoryResponseItem{Created: 0},
trunc: false,
human: true,
}, "", ctx.CreatedSince,
},
{
// A time before the year 2000 is not displayed.
historyContext{
h: image.HistoryResponseItem{Created: time.Date(1980, time.November, 10, 10, 23, 0, 0, time.UTC).Unix()},
trunc: false,
human: true,
}, "", ctx.CreatedSince,
},
{
// A time after 2000 is displayed.
historyContext{
h: image.HistoryResponseItem{Created: time.Date(2005, time.September, 27, 4, 37, 0, 0, time.UTC).Unix()},
trunc: false,
human: true,
}, "", ctx.CreatedSince,
},
}

for _, c := range cases {
Expand Down Expand Up @@ -173,6 +197,7 @@ func TestHistoryContext_Comment(t *testing.T) {
func TestHistoryContext_Table(t *testing.T) {
out := bytes.NewBufferString("")
unixTime := time.Now().AddDate(0, 0, -1).Unix()
oldDate := time.Now().AddDate(-17, 0, 0).Unix()
histories := []image.HistoryResponseItem{
{
ID: "imageID1",
Expand All @@ -185,19 +210,25 @@ func TestHistoryContext_Table(t *testing.T) {
{ID: "imageID2", Created: unixTime, CreatedBy: "/bin/bash echo", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
{ID: "imageID3", Created: unixTime, CreatedBy: "/bin/bash ls", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
{ID: "imageID4", Created: unixTime, CreatedBy: "/bin/bash grep", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
{ID: "imageID5", Created: 0, CreatedBy: "/bin/bash echo", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
{ID: "imageID6", Created: oldDate, CreatedBy: "/bin/bash echo", Size: int64(182964289), Comment: "Hi", Tags: []string{"image:tag2"}},
}

const expectedNoTrunc = `IMAGE CREATED CREATED BY SIZE COMMENT
imageID1 24 hours ago /bin/bash ls && npm i && npm run test && karma -c karma.conf.js start && npm start && more commands here && the list goes on 183MB Hi
imageID2 24 hours ago /bin/bash echo 183MB Hi
imageID3 24 hours ago /bin/bash ls 183MB Hi
imageID4 24 hours ago /bin/bash grep 183MB Hi
imageID5 /bin/bash echo 183MB Hi
imageID6 17 years ago /bin/bash echo 183MB Hi
`
const expectedTrunc = `IMAGE CREATED CREATED BY SIZE COMMENT
imageID1 24 hours ago /bin/bash ls && npm i && npm run test && kar… 183MB Hi
imageID2 24 hours ago /bin/bash echo 183MB Hi
imageID3 24 hours ago /bin/bash ls 183MB Hi
imageID4 24 hours ago /bin/bash grep 183MB Hi
imageID5 /bin/bash echo 183MB Hi
imageID6 17 years ago /bin/bash echo 183MB Hi
`

cases := []struct {
Expand Down

0 comments on commit 06cbd26

Please sign in to comment.