func (c *usersCommand) apiUsersToUserInfoSlice(users []params.ModelUserInfo) []UserInfo { var output []UserInfo for _, info := range users { outInfo := UserInfo{Username: info.UserName} outInfo.DateCreated = user.UserFriendlyDuration(info.DateCreated, time.Now()) if info.LastConnection != nil { outInfo.LastConnection = user.UserFriendlyDuration(*info.LastConnection, time.Now()) } else { outInfo.LastConnection = "never connected" } output = append(output, outInfo) } return output }
func (*userFriendlyDurationSuite) TestFormat(c *gc.C) { now := time.Now() for _, test := range []struct { other time.Time expected string }{ { other: now, expected: "just now", }, { other: now.Add(-1 * time.Second), expected: "just now", }, { other: now.Add(-2 * time.Second), expected: "2 seconds ago", }, { other: now.Add(-59 * time.Second), expected: "59 seconds ago", }, { other: now.Add(-60 * time.Second), expected: "1 minute ago", }, { other: now.Add(-61 * time.Second), expected: "1 minute ago", }, { other: now.Add(-2 * time.Minute), expected: "2 minutes ago", }, { other: now.Add(-59 * time.Minute), expected: "59 minutes ago", }, { other: now.Add(-60 * time.Minute), expected: "1 hour ago", }, { other: now.Add(-61 * time.Minute), expected: "1 hour ago", }, { other: now.Add(-2 * time.Hour), expected: "2 hours ago", }, { other: now.Add(-23 * time.Hour), expected: "23 hours ago", }, { other: now.Add(-24 * time.Hour), expected: now.Add(-24 * time.Hour).Format("2006-01-02"), }, { other: now.Add(-96 * time.Hour), expected: now.Add(-96 * time.Hour).Format("2006-01-02"), }, } { obtained := user.UserFriendlyDuration(test.other, now) c.Check(obtained, gc.Equals, test.expected) } }
func apiUsersToUserInfoMap(users []params.ModelUserInfo) map[string]UserInfo { output := make(map[string]UserInfo) for _, info := range users { outInfo := UserInfo{ DisplayName: info.DisplayName, Access: string(info.Access), } if info.LastConnection != nil { outInfo.LastConnection = user.UserFriendlyDuration(*info.LastConnection, time.Now()) } else { outInfo.LastConnection = "never connected" } output[info.UserName] = outInfo } return output }