Skip to content

Commit

Permalink
Merge pull request #54 from meltred/time
Browse files Browse the repository at this point in the history
feat: using since time for showing times and dates
  • Loading branch information
KunalSin9h committed Dec 24, 2023
2 parents 0389e3f + d9767a1 commit 4ae95aa
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 32 deletions.
44 changes: 43 additions & 1 deletion cmd/meltcd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,55 @@ func getAllApplications(_ *cobra.Command, _ []string) error {
table.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

for _, v := range resPayload.Data {
table.AddRow(v.ID, v.Name, v.Health, v.LastSyncedAt.Format(time.RFC822), v.CreatedAt.Format(time.RFC822), v.UpdatedAT.Format(time.RFC822))
table.AddRow(v.ID, v.Name, v.Health, getSinceTime(v.LastSyncedAt), getSinceTime(v.UpdatedAT), getSinceTime(v.CreatedAt))
}

table.Print()
return nil
}

func getSinceTime(t time.Time) string {
elapsed := time.Since(t).Milliseconds()

if elapsed == 0 {
return "now"
}

sec := elapsed / 1000
mins := sec / 60
hrs := mins / 60
days := hrs / 24
weeks := days / 7
months := weeks / 4
year := months / 12

if year != 0 {
return fmt.Sprintf("%d year ago", year)
}

if months != 0 {
return fmt.Sprintf("%d months ago", months)
}

if weeks != 0 {
return fmt.Sprintf("%d weeks ago", weeks)
}

if days != 0 {
return fmt.Sprintf("%d days ago", days)
}

if hrs != 0 {
return fmt.Sprintf("%d hours ago", hrs)
}

if mins != 0 {
return fmt.Sprintf("%d minutes ago", mins)
}

return "now"
}

func getSpecFromData(cmd *cobra.Command, args []string) (application.Spec, error) {
var spec application.Spec

Expand Down
1 change: 1 addition & 0 deletions internal/core/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func Register(app *application.Application) error {
timeOfCreation := time.Now()
app.CreatedAt = timeOfCreation
app.UpdatedAt = timeOfCreation
app.LastSyncedAt = timeOfCreation

log.Info("Registered!")
return nil
Expand Down
Loading

0 comments on commit 4ae95aa

Please sign in to comment.